SoFunction
Updated on 2025-03-04

Description of the difference between location proxy_pass addition and non-adding/ in Nginx

1. Location configuration

Prefix matching

# Match URIs starting with /images/location /images/ {
    # Corresponding processing configuration}
  • No suffix/hour,location /abc/defCan match/abc/defghiRequests can also match/abc/def/ghiwait
  • The suffix has/hour,location /abc/def/Can't match/abc/defghiRequests can only match/abc/def/anythingSuch a request

Exact match

# Exact match is only the URI of /loginlocation = /login {
    # Perform corresponding processing}

Regular expression matching

# Regularly match URIs like /user/123location ~ ^/user/\d+ {
    # Perform corresponding processing}

Wildcard matching

# Match URIs ending with .jpg, .png, or .giflocation ~* \.(jpg|png|gif)$ {
    # Perform corresponding processing}

use~* When performing regular expression matching, it means that the requested URI is case-insensitive.

2. proxy_pass configuration

Without suffix “/”

location /api {
    # The proxy only replaces schema://ip:port    proxy_pass http://backend_server;
}

In this case, if the original requested URI is/api/foo, the Nginx proxy forwards the request tohttp://backend_server/api/foo

Nginx does not modify the original requested URI, but passes it intact to the backend server.

Add the suffix “/”

location /api {
    # The proxy replaces the entire prefix, including the prefix used by location matching.  That is, schema://ip:port/[location pattern]    proxy_pass http://backend_server/;
}

In this case, if the original requested URI is/api/foo, the Nginx proxy forwards the request tohttp://backend_server/foo

Nginx will put the original requested URI inlocationThe prefix is ​​removed and the remaining part is appended to the backend server address configured in the proxy_pass directive.

When location is a regular expression, the end of proxy_pass cannot be carried/, otherwise the error will be reported as follows

nginx: [emerg] “proxy_pass” cannot have URI part in location given by regular expression, or inside named location, or inside “if” statement, or inside “limit_except” block in

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.