To disable OPTIONS requests in Nginx and allow cross-origin, you can follow the tutorial below to configure:
-
Open the Nginx configuration file:
Execute the following command in the terminal to edit the Nginx configuration file:sudo nano /etc/nginx/nginx.conf
-
Add configuration to disable OPTIONS requests:
Insidehttp
the block add the following configuration:server { listen 80; server_name example.com; # 替换为您的域名或IP地址 location / { if ($request_method = OPTIONS) { return 403; } # 其他配置项... } }
will
example.com
be replaced with your domain name or IP address. -
Add configuration to allow cross-origin requests:
Insidehttp
the 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.com
be replaced with your domain name or IP address. - Save and close configuration file:
PressCtrl + X
, then EnterY
to save changes and close the editor. -
Test whether the configuration is correct:
Execute the following command in the terminal to verify whether the Nginx configuration is correct:sudo nginx -t
-
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.