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:
View source
Print?
HTTP/1.1 200 OK
Server: nginx
Access-Control-Allow-Origin: other.subdomain.com