Home>

When performing nginx reverse proxy configuration,Slashes in location and proxy_pass can cause all kinds of confusion,Sometimes one more or one slash,Will lead to completely different results,Therefore, the situation of location and proxy_pass with or without slashes is specifically arranged and combined.Conducted a complete test,Find out the principle,To improve posture level ~

〇. Environmental Information

Two nginx servers

nginx a:192.168.1.48

nginx b:192.168.1.56

I. Test method

Configure different rules in nginx a,Then request nginx a:http://192.168.1.48/foo/api

Observe the requests received by nginx b,The specific operation is to view the $request field in the log

Test process and results

Case 1

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/;
}

Request received by nginx b:/api

Case 2

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/;
}

The request received by nginx b://api

Case 3

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/;
}

Request received by nginx b:/foo/api

Case 4

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/;
}

Request received by nginx b:/foo/api

Case 5

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/bar/;
}

Request received by nginx b:/bar/api

Case 6

nginx a configuration:

location/foo {
  proxy_pass http://192.168.1.56/bar/;
}

The request received by nginx b:/bar //api

Case 7

nginx a configuration:

location/foo/{
  proxy_pass http://192.168.1.56/bar;
}

Request received by nginx b:/barapi

Case 8

nginx a configuration:

location/foo {
  proxy_pass http://192.168.1.56/bar;
}

Request received by nginx b:/bar/api

Seeing all dizzy here?Is actually regular

Now sort these cases into tables,The result indicates the request received by nginx b

Table I

Case location proxy_pass result
1 /foo / http://192.168.1.48/ /api
2 /foo http://192.168.1.48/ //api
3 /foo / http://192.168.1.48 /foo/api
4 /foo http://192.168.1.48 /foo/api

Table II

Case location proxy_pass result
5 /foo / http://192.168.1.48/bar/ /bar/api
6 /foo http://192.168.1.48/bar/ /bar //api
7 /foo / http://192.168.1.48/bar /barapi
8 /foo http://192.168.1.48/bar /bar/api

Three. Analysis

Original request path:unified as "/foo/api" in this article

location:The location column in the table above

proxy_pass:the proxy_pass column in the table above

New request path:The string after nginx processed the original request path

Focus on the analysis of proxy_pass,Can be divided into 3 forms

Then according to whether the string is connected to ip:port, it is classified into 2 types, and "/" is also a string.So 1 falls into one category,2, 3 fall into one category,The two types of situations are explained below

When there is no string after the ip:port of proxy_pass,nginx will pass the original request path to the next nginx intact, as in cases 3 and 4

When the ip:port of proxy_pass is followed by a string,nginx will remove the location from the original request path, and then stitch the remaining strings to proxy_pass to generate a new request path.Then forward the new request path to the next stop nginx (the above situation is actually the same as this,It's just that the removed string is an empty string ~~)

Take the most confusing example:Case 7. The ip:port of proxy_pass is followed by the string "/bar", so the location:"/foo/" is removed from the original request path:"/foo/api" to become "api", and then "api" is spliced Go to proxy_pass:http://192.168.1.48/bar generated a new request url:"http://192.168.1.48/barapi", so the next nginx request is "/barapi".

Case 6:The ip:port of proxy_pass is followed by the string "/bar/", so the location:"/foo" is removed from the original request path "/foo/api" and becomes "/api", then "/api "spliced ​​to proxy_pass:http://192.168.1.48/bar/generated a new request path:" http://192.168.1.48/bar//api ", so the next nginx request received is/bar //api.

Other cases can be deduced by analogy.Now I finally figured it out,No more fog.

  • Previous Kd tree implementation of python K-nearest neighbor algorithm
  • Next Examples of jQuery to achieve animation, disappear, appear, fade out, fade in effect