How to configure nginx to allow cross-origin requests?
To allow cross-origin access in Nginx, follow these steps:
- The nginx configuration file can be found at /etc/nginx/nginx.conf.
- The configuration file for Nginx in the directory /etc/nginx/conf.d/default.conf.
- 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;
}
...
}
...
}
- 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.