In-depth analysis of Nginx cross-origin AJAX requests, nginxajax
Cross-origin occurs when AJAX requests from one domain to another. So how can I implement ajax cross-origin requests on nginx? To enable cross-origin requests on nginx, add the add_header Access-Control * command. As follows:
Location/{add_header 'access-Control-Allow-origin' 'HTTP: // other.subdomain.com '; add_header 'access-Control-Allow-Credentials' 'true '; add_header 'access-Control-Allow-Methods ''' GET ';...... the rest of your configuration here ......}
Note:
First Command: authorize requests from other.subdomain.com
Second command: when the flag is true, the response is to whether the request can be exposed.
The third day command: Specifies the request method, which can be GET or POST.
If you want to allow access from any domain, you can configure it as follows:
Access-Control-Allow-Origin :*
Restart nginx
Service nginx reload
Ajax cross-origin request test
When the request is successful, the response header is as follows:
HTTP/1.1 200 OKServer: nginxAccess-Control-Allow-Origin: other.subdomain.com