How to configure nginx to allow cross-origin requests?

To allow cross-origin access in Nginx, follow these steps:

  1. The nginx configuration file can be found at /etc/nginx/nginx.conf.
  2. The configuration file for Nginx in the directory /etc/nginx/conf.d/default.conf.
  3. The protocol that allows communication between devices on the internet
http {
    ...
    server {
        ...
        location / {
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header Access-Control-Allow-Credentials true;
        }
        ...
    }
    ...
}
  1. Save the configuration file and restart the Nginx service.

After this configuration, Nginx will add Access-Control-Allow-* related header information in the response headers, allowing cross-origin access. Please note that setting Access-Control-Allow-Origin to * means allowing all domain names to have cross-origin access. If you only want to allow specific domain names to have cross-origin access, you can replace * with the specific domain name.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds