SoFunction
Updated on 2025-04-14

Detailed explanation of the matching rules of location and proxy_pass in nginx

I'll explain in detaillocationandproxy_passWhat are their roles and what they mean in your examples.

The role of location

locationIt 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/testAt the beginning, thislocationBlocks will be matched.
    • For example, request/testor/test/abcThey will all enter this block.
  • URI processing
    • By default, Nginx will put the requested full URI (including/testPart) passed to the backend unlessproxy_passThere are special configurations.

The role of proxy_pass

proxy_passSpecifies 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 matchlocation /testThe request is forwarded to/tt
    • The backend server will receive forwarded requests, and the specific URI depends on the configuration details.

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 /testMatch requested/testPrefix.
    • Nginx will/testThe latter part (/abc) Keep it.
    • proxy_pass /ttSpecify the target address as/ttand 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_passAdd 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 notlocationof/testPartially 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.

No specific path

location /test {
    proxy_pass ;
}
  • Client Request/test/abc
  • Forward to the backend/test/abc
  • explain
    • becauseproxy_passNo specific path (only domain names), Nginx will add the complete URI of the client (/test/abc) Pass directly to the backend.

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/abcForward to/abc
  • /test/It is removed, only the latter part is retained.
  • Regular Match
location ~ ^/test/(.*)$ {
    proxy_pass /tt/$1;
}
  • ask/test/abcForward to/tt/abc
  • Use regular capture groups$1Dynamic delivery path.

Summarize

  • location /test: Match with/testThe beginning request.
  • proxy_pass /tt: Forward the request to/tt, retained by default/testThe path after  and append to/ttback.
  • 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!