I'll explain in detaillocation
andproxy_pass
What are their roles and what they mean in your examples.
The role of location
location
It is an instruction in Nginx that matches the client request URI, which determines how to handle requests for a specific path. It defines the routing rules for requests and subsequent configurations (such asproxy_pass
) will be applied to the matching request.
grammar
location [Modifier] Matching mode { #Configuration Block}
-
Modifier: Optional, for example
=
(Exact match),~
(regular matching) etc. - Matching mode: It can be a specific path, regular expression, etc.
Example: location /test
location /test { proxy_pass ; }
-
meaning:
- When the client requests the URI with
/test
At the beginning, thislocation
Blocks will be matched. - For example, request
/test
or/test/abc
They will all enter this block.
- When the client requests the URI with
-
URI processing:
- By default, Nginx will put the requested full URI (including
/test
Part) passed to the backend unlessproxy_pass
There are special configurations.
- By default, Nginx will put the requested full URI (including
The role of proxy_pass
proxy_pass
Specifies the backend server address (can be a domain name, IP, or upstream server group) to which the request will be proxyed. It defines the destination of the request.
grammar
proxy_pass protocol://Destination address;
Example: proxy_pass /tt
location /test { proxy_pass /tt; }
-
meaning:
- Will match
location /test
The request is forwarded to/tt
。 - The backend server will receive forwarded requests, and the specific URI depends on the configuration details.
- Will match
Combined location /test and proxy_pass /tt
Let me explain the effect of the combination of these two:
Default behavior (with path replacement)
location /test { proxy_pass /tt; }
-
Client Request:
/test/abc
-
Forward to the backend:
/tt/abc
-
explain:
-
location /test
Match requested/test
Prefix. - Nginx will
/test
The latter part (/abc
) Keep it. -
proxy_pass /tt
Specify the target address as/tt
and put the remaining paths (/abc
) append to the back. - What the backend finally receives is
/tt/abc
。
-
The effect of adding slashes (not replacing paths)
If inproxy_pass
Add slashes to the back:
location /test { proxy_pass /tt/; }
-
Client Request:
/test/abc
-
Forward to the backend:
/tt/abc
-
explain:
- Added slashes
/
After that, Nginx will notlocation
of/test
Partially replace it, but directly replace the remaining paths of the request (/abc
) Append to/tt/
later. - The result is the same as above, but the logic is clearer.
- Added slashes
No specific path
location /test { proxy_pass ; }
-
Client Request:
/test/abc
-
Forward to the backend:
/test/abc
-
explain:
- because
proxy_pass
No specific path (only domain names), Nginx will add the complete URI of the client (/test/abc
) Pass directly to the backend.
- because
Common configuration modes
- Exactly match paths:
location = /test { proxy_pass /tt; }
- Match only
/test
, won't match/test/abc
。 - Forward to
/tt
(without extra paths).
- Remove the prefix:
location /test/ { proxy_pass /; }
- ask
/test/abc
Forward to/abc
。 -
/test/
It is removed, only the latter part is retained.
- Regular Match:
location ~ ^/test/(.*)$ { proxy_pass /tt/$1; }
- ask
/test/abc
Forward to/tt/abc
。 - Use regular capture groups
$1
Dynamic delivery path.
Summarize
-
location /test
: Match with/test
The beginning request. -
proxy_pass /tt
: Forward the request to/tt
, retained by default/test
The path after and append to/tt
back. -
Key points: Whether to add slashes (
/
) Whether to use regularity will affect the way the path is passed.
This is the article about the matching rules of nginx `location` and `proxy_pass`. For more related contents of nginx `location` and `proxy_pass`, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!