How to resolve the issue of nginx configuration not working for cross-domain requests?

To resolve the issue of the nginx configuration cross-origin not taking effect, you can try the following steps:

  1. example.com can be paraphrased as “a website called example”.
location / {
    add_header Access-Control-Allow-Origin http://example.com;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'Authorization, Content-Type';
    add_header Access-Control-Allow-Credentials 'true';
}
  1. Check if the syntax of the nginx configuration file is correct by using the following command:
nginx -t
  1. Restart the nginx service to apply the configuration changes.
systemctl restart nginx
  1. Make sure to clear the browser cache or try opening the webpage in incognito mode.

If none of the above steps resolve the issue, you can try checking the response headers of the request in the browser’s developer tools to see if the correct cross-origin settings are included.

广告
Closing in 10 seconds
bannerAds