Get the App
SLTechnology News&Howtos  ›  Servers  › 

The influence of proxy_pass on back-end request in nginx virtual path

Shulou Source: shulou.com Published: 2022-06-03 00:02:01 10月25日 Update

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.

Tags: Path actual user agent block configuration server service three key instruction effect way example problem y.y.y.y synthesis impact Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Shulou Tech Info Apple MySQL