Nginx server reverse proxy proxy_pass configuration method explained _nginx_ script home

Nginx server reverse proxy proxy_pass configuration method explains

Updated: January 27, 2016 10:51:03 by vtrtbb
This article mainly introduces the Nginx server reverse proxy proxy_pass configuration method, including frequently mentioned url/problem related instructions, need friends can refer to

In terms of ordinary reverse proxy, the configuration of Nginx is relatively simple, such as:

location ~ /* {proxy_pass http://127.0.0.1:8008; }

Or can

location / {proxy_pass http://127.0.0.1:8008; }

The configuration of the Apache2 reverse proxy is as follows:

ProxyPass /ysz/ http://localhost:8080/

However, if you want to configure a relatively complex reverse proxy Nginx relative to Apache2 is a little more troublesome, for example, the url starts with /wap/ request forward to the background corresponding to a server can then set a variable in Nginx, to temporarily save the path information after /wap/

location ^~ /wap/
{
if ($request_uri ~ /wap/(\d+)/(.+))
{
set $bucketid $1;
set $params $2;
}
proxy_pass http://mx$bucketid.test.com:6601/$params;
}

Or you can rewrite first, and then delegate:

location ^~ /wap/{ rewrite /wap/(\d+)/(.+) /$2? $args break; proxy_pass http://mx$1.test.com:6601; }

perhaps

location ~* /wap/(\d+)/(.+)
{
proxy_pass http://mx$1.test.com:6601/$2?$args;
}

Notice the last one at the top? If you use a variable in proxy_pass (either the hostname variable $1 or the $2 variable after it), you must add this code, but if pass_proxy doesn't use any variables, you don't need to add this code. By default, it will give all urls to the proxy in the background, such as:

location ~* /wap/(\d+)/(.+)
{
proxy_pass http://mx.test.com:6601;
}

Apache2 is much simpler:

ProxyPassMatch ^ / wap/(. *) $http://192.168.132.147/$1 if ($host WWW. ~ * (. *)) {set $host_without_www $1; rewrite (.*)$ http://$host_without_www/www$1; }

When proxy_pass is configured in nginx, when/is added to the following url, which is equivalent to the absolute root path, nginx will not proxy the matched path in location; If there is no /, the matching part of the path is also given to the agent. The following four cases with http://192.168.1.4/proxy/test.html for a visit. The first:

location /proxy/ {proxy_pass http://127.0.0.1:81/; }

Is the agent to the url http://127.0.0.1:81/test.html second za (relative to the first one, last one less /)

location /proxy/ {proxy_pass http://127.0.0.1:81; }

Third: is the agent to the url http://127.0.0.1:81/proxy/test.html

The location/proxy / {proxy_pass http://127.0.0.1:81/ftlynx/; }

Is the agent to the url http://127.0.0.1:81/ftlynx/test.html. Fourth case (last one less/than third) :

The location/proxy / {proxy_pass http://127.0.0.1:81/ftlynx; }

Is the agent to the url http://127.0.0.1:81/ftlynxtest.html the results above are my combining with log files have been tested. As can be seen from the results, it is correct to say that there are two cases. The http://127.0.0.1:81 (the second) the and http://127.0.0.1:81/... (1,3,4 above) This.

Related article

Latest comments