Nginx settings disable OPTIONS requests and allow cross-domain tutorials!

To disable OPTIONS requests in Nginx and allow cross-origin, you can follow the tutorial below to configure:

  1. Open the Nginx configuration file:
    Execute the following command in the terminal to edit the Nginx configuration file:

    sudo nano /etc/nginx/nginx.conf
  2. Add configuration to disable OPTIONS requests:
    Inside  httpthe block add the following configuration:

    server {
        listen 80;
        server_name example.com;  # 替换为您的域名或IP地址
    
        location / {
            if ($request_method = OPTIONS) {
                return 403;
            }
    
            # 其他配置项...
        }
    }

    will  example.combe replaced with your domain name or IP address.

  3. Add configuration to allow cross-origin requests:
    Inside  httpthe block add the following configuration:

    server {
        listen 80;
        server_name example.com;  # 替换为您的域名或IP地址
    
        location / {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
    
            # 其他配置项...
        }
    }

    will  example.combe replaced with your domain name or IP address.

  4. Save and close configuration file:
    Press  Ctrl + X, then Enter  Yto save changes and close the editor.
  5. Test whether the configuration is correct:
    Execute the following command in the terminal to verify whether the Nginx configuration is correct:

    sudo nginx -t
  6. Restart Nginx:
    Execute the following command in the terminal to restart the Nginx service:

    sudo service nginx restart

Disable OPTIONS requests and allow cross-origin settings in Nginx by following the steps above. Please make sure to use the correct server name or IP address when editing the Nginx configuration file.

Guess you like

Origin blog.csdn.net/tiansyun/article/details/131485615