Nginx implements AJAX cross-domain requests
Ajax requests from one domain to another domain will have cross-domain issues. So how to implement Ajax cross-domain request on Nginx? To enable cross-domain requests on nginx, you need to add the Add_header access-control* directive. As shown below:
?
123456789101112 |
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 ... ... } |
Comments are as follows:
- First instruction: authorizing requests from other.subdomain.com
- Second instruction: When the flag is true, responds to whether the request can be exposed
- The third day directive: Specifies the method of the request, which can be get,post, etc.
If you need to allow access from any domain, you can configure this:
?
1 |
Access-Control-Allow-Origin: * |
Re-start Nginx
?
Ajax cross-domain request testing
Upon success, the response header is as follows:
?
123 |
HTTP/1.1 200 OK Server: nginx Access-Control-Allow-Origin: other.subdomain.com |
Solving the cross-domain problem of Ajax with Nginx and Apache reverse proxy
One of the most attractive features on the main station is the count of downloads, as shown in. This feature is implemented using the technology described above.
As you can see from the Firebug, this page repeatedly requests a path named/api/counter to get the latest number of downloads through Ajax.
And this output path actually does not exist on the server, this path is just another server some path, this is the use of Nginx reverse proxy function.
1, Nginx
Back to this example of the counter, Nginx configuration fragment is as follows:
?
12345 |
location /api/counter { rewrite (.*) /out break; proxy_pass http://hfahe.maxthon.com; proxy_set_header Host "hfahe.maxthon.com"; } |
Then access http://www.maxthon.cn/api/counter this address, output and direct access to http://hfahe.maxthon.com/out this address is exactly the same as shown in. In this way, local Ajax will be able to read data from other remote servers.
The Proxy_set_header parameter is used when forwarding a domain name is required. Nginx can also carry out port forwarding, just Proxy_pas
The S configuration is modified to http://hfahe.maxthon.com:81 this form.
2. Apache
Apache reverse proxies need to use modules such as Mod_proxy and mod_proxy_http.so.
The configuration under Windows is as follows:
?
12345678 |
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so ProxyRequests Off ProxyPass /start http://i.maxthon.cn/ ProxyPass /proxy http://192.168.1.111/proxy/ ProxyPassReverse /proxy http://192.168.1.111/proxy/ # for server redirect |
The Proxypass and Proxypassreverse directives are the configuration required by the reverse proxy. Proxypass is used to map a remote server to the URL space of the local server. Proxypassreverse mainly addresses the problem of bypassing the reverse proxy caused by back-end server redirection, which is used when the backend server is server-side jump, location in response to HTTP redirection, Content-location and URLs in the URI header are adjusted.
The configuration under Linux is as follows:
?
123456 |
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so ProxyRequests Off ProxyPass /fb http://fb.maxthon.com/ajax ProxyPassReverse /fb http://192.168.1.111/proxy/ # for server redirect |
How to handle Ajax cross-domain request configuration in Nginx server