Location>code7788 >text

Nginx server configuration - forwarding rules for proxy_pass in reverse proxy service

Popularity:356 ℃/2024-11-20 17:35:56

nginx is a http web server developed in Russia, we often use this server for load balancing and reverse proxy.
Today we'll talk about how to configure routing when Nginx is used as a reverse proxy.
Assuming you've deployed Nginx, let's go to the Nginx installation directory and go to File.
Find the server node under the http node, the value is a json.
There is a location directive in the json that stands for forwarding.
It's usually in this form:

location {$path} {
        proxy_pass {$url};
}

The {$path} represents the part that matches the source url, and the {$url} after proxy_pass represents the destination url to be forwarded.

The question of whether or not to carry the original path when forwarding is generally addressed here. An example:

location /abc {
        proxy_pass http://127.0.0.1:9090/;
}

If we request http://127.0.0.1:80/abc (assuming nginx's server's is 80)

Then the path path of the request is /abc, then it will match the rule /abc of this location directive, then the request will be forwarded to the local port 9090.
If we request http://127.0.0.1:80/abc/cloud
Then the request path path is /abc/cloud, (anti-theft connection: this article was first published from /jilodream/ ) at this time will match the rules of the location directive, then the request will be forwarded to the machine's port 9090.
But the question arises, when forwarding port 9090, should /abc be appended to the back? Should the /cloud part at the back be appended?
This is related to whether or not $url contains a path, no slash means it does not contain a path, a slash means it contains a path.

One,

If: $url is http://127.0.0.1:9090 means no path
In this case, the path portion of the source url is appended directly to the
A few examples
(1)

location /abc {
        proxy_pass http://127.0.0.1:9090;
}

Requests http://127.0.0.1:80/abc/bcd

Then jump to http://127.0.0.1:9090/abc/bcd
(2)

location /abc/bcd {
        proxy_pass http://127.0.0.1:9090;
}

Requests http://127.0.0.1:80/abc/bcd

Then jump to http://127.0.0.1:9090/abc/bcd
(3)

location /abc/bcd/ {
        proxy_pass http://127.0.0.1:9090;
}

Requests http://127.0.0.1:80/abc/bcd/

Then jump to http://127.0.0.1:9090/abc/bcd/

To summarize, the target url configured after proxy_pass will append the path of the source url directly to the target url if there is no path information (including /).

Two,

If: $url is http://127.0.0.1:9090/ means path is available
If: $url is http://127.0.0.1:9090/gov means there is a path
If: $url is http://127.0.0.1:9090/gov/ means there is a path
In this case, the path part of the source url will be removed from the matched part, and the remaining part will be appended directly to the target url, as shown in the figure:

 

A few examples

(1)

location /abc/ {
        proxy_pass http://127.0.0.1:9090/;
}

Requests http://127.0.0.1:80/abc/bcd

Then jump to http://127.0.0.1:9090/bcd
Analysis:
The path part of the source url is: "/abc/bcd"
Successful match with match rule "/abc/".
Remainder after matching is "bcd" "http://127.0.0.1:9090/" append "bcd"
Then you will end up at http://127.0.0.1:9090/bcd
(2)

location /abc {
        proxy_pass http://127.0.0.1:9090/gov;
}

Requests http://127.0.0.1:80/abc/bcd

Then jump to http://127.0.0.1:9090/gov/bcd
(3)

location /abc/b {
        proxy_pass http://127.0.0.1:9090/gov/;
}

Requests http://127.0.0.1:80/abc/bcd/

Then jump to http://127.0.0.1:9090/gov/cd/
(4)

location /abc/b {
        proxy_pass http://127.0.0.1:9090/gov/;
}

Requests http://127.0.0.1:80/abc/b/cd/

Then jump to http://127.0.0.1:9090/gov//cd/

To summarize, the target url configured after proxy_pass, if there is path information (including /), the remaining part of the path after the partial match of the source url will be appended directly to the target url.

Now another question is, (anti-theft link: this article was first published from /jilodream/ ) if there is more than one match rule that hits, then what will nginx do with it?

Below:

location / {
        proxy_pass http://127.0.0.1:9091/gov/;
}
location /abc {
        proxy_pass http://127.0.0.1:9092/gov/;
}
location /abc/ai {
        proxy_pass http://127.0.0.1:9093/gov/;
}

Requests http://127.0.0.1:80/abc/ai/

Then nginx will select a match according to the maximum match principle, and then forward the request to port 9093