Nginx implementation Ajax Cross-domain request
Ajax requests another domain from one domain to have cross-domain problems. So how do you implement the Ajax Cross-domain request on the Nginx? To enable Cross-domain requests on nginx, you need to add add_header access-control* directives. As shown below:
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
...
...
}
The comments are as follows:
- First directive: authorizing requests from other.subdomain.com
- Second instruction: When the flag is true, it responds to whether the request can be exposed
- Third day directive: Specifies the method of the request, can be get,post, etc.
If you need to allow access from any domain, you can configure this:
Access-control-allow-origin: *
Restart Nginx
Ajax cross-domain request testing
When successful, the response header looks like this:
http/1.1 OK
Server:nginx
access-control-allow-origin:other.subdomain.com
solving Ajax cross-domain problems with Nginx and Apache reverse proxies
Proud of the main station has a very attractive function, that is, the number of downloads count, as shown in the following figure. This function is to use the above technology to achieve.
As you can see from the Firebug in the following figure, the page repeatedly requests a path named/api/counter to get the latest download number through Ajax.
And this output path actually does not exist on the server, this path is just another server a path only, this is uses the Nginx reverse proxy function realization.
1, Nginx
Back to this example of the counter, the Nginx configuration fragment looks like this:
location/api/counter {
rewrite (. *)/out break;
Proxy_pass http://hfahe.maxthon.com;
Proxy_set_header Host "hfahe.maxthon.com";
}
So access to http://www.maxthon.cn/api/counter this address, output and direct access http://hfahe.maxthon.com/out This address is exactly the same, as shown in the following figure. In this way, local AJAX can read data from other remote servers.
The Proxy_set_header parameter is used when forwarding of domain names is required. Nginx can also be forwarded to the port simply by Proxy_pas
s configuration modified to http://hfahe.maxthon.com:81 this form can be.
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:
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
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. The Proxypassreverse mainly solves the problem of bypassing the reverse proxy caused by back-end server redirection, when the back-end server makes server-side jumps, the location in response to HTTP redirection, Content-location and the URL in the URI header are adjusted.
The configuration under Linux is shown below:
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