The influence of proxy_pass on back-end request in nginx virtual path
Suppose the configuration in nginx looks like this:
Server {
Listen 80
Server_name x.x.x.x
. . . . . .
Location / subdir
{
Proxy_pass http://y.y.y.y;
}
}
Then, when the user requests http://x.x.x.x/subdir/other, matching to that block, nginx reverse proxies to the backend will retain the virtual path. The request URL that nginx actually initiates to the backend is http://y.y.y.y/subdir/other.
Suppose the configuration in nginx looks like this:
Server {
Listen 80
Server_name x.x.x.x
. . . . . .
Location / subdir
{
Proxy_pass http://y.y.y.y/;
}
}
Then, when the user requests http://x.x.x.x/subdir/other, the block is matched, and because the backend URI path is specified in the proxy_pass, the nginx agent will not retain the virtual path when it requests to the backend. The request URL that nginx actually initiates to the backend is http://y.y.y.y//other.
Suppose the configuration in nginx looks like this:
Server {
Listen 80
Server_name x.x.x.x
. . . . . .
Location / subdir
{
Proxy_pass http://y.y.y.y/upstream_subdir;
}
}
Then, when the user requests http://x.x.x.x/subdir/other, the block is matched, and because the backend URI path is specified in the proxy_pass, the nginx agent will not retain the virtual path when it requests to the backend. The request URL that nginx actually initiates to the backend is http://y.y.y.y/upstream_subdir/other.
Summing up the above three examples, we can find that the crux of the problem is whether the URI of the specified backend server is specified in the proxy_pass instruction, which determines whether the virtual path in the location will be retained when the nginx proxy requests to the backend.
When the back-end server is written in upstream mode, the effect is the same. For example:
Upstream backend {
Server y.y.y.y:80
}
Server {
Listen 80
Server_name x.x.x.x
. . . . . .
Location / subdir
{
Proxy_pass http://backend/upstream_subdir;
}
}
When a user requests a http://x.x.x.x/subdir/other, the actual request URL that the nginx initiates to the backend is still http://y.y.y.y/upstream_subdir/other.