nginx magic” in reverse proxy
When configuring the nginx reverse proxy, the slash in location and proxy_pass causes all sorts of problems. Sometimes one more slash or one less slash causes completely different results, so specially after location and proxy_passIn the case of no diagonal line, the arrangement and combination are made, and a complete test is carried out to find out the principle, so as to improve the posture level.
> test method.
- Configure different rules in nginx A, then request nginx A:http://192.168.1.48/foo/api
- Observe the request received by nginx B. The specific operation is to look at the $request field in the log.
case 1
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx BRequest received: /api
case 2
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx BRequest received: //api
case 3
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx BRequest received: /foo/api
Case 4
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx BRequest received: /foo/api
case 5
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/bar/;
}
nginx BRequest received: /bar/api
case 6
nginx ATo configure:
location /foo {
proxy_pass http://192.168.1.56/bar/;
}
nginx BRequest received: /bar//api
case 7
nginx ATo configure:
location /foo/ {
proxy_pass http://192.168.1.56/bar;
}
nginx BRequest received: /barapi
case 8
nginx ATo configure:
location /foo {
proxy_pass http://192.168.1.56/bar;
}
nginx BRequest received: /bar/api
Do you feel dizzy here? Is it regular?
Now, these cases are arranged in a table, which indicates the request received by nginx B.
Table 1
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 two
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 |