配置nginx反向代理时,总是需要尝试多次才能配置成功,通过本文做个记录,方便以后查看。
1. proxy_pass 只有主机地址时
这种情况下,相当于下面的公式:
backend url = proxy_pass + path
这种情况下,请求的 path 部分会直接追加到 proxy_pass 地址后,相当于把nginx地址一对一的映射到了后端地址,这种配置方式理解最简单。
location |
proxy_pass |
path |
nginx url |
backend url |
/ |
http://backend |
/ |
http://nginx/ |
http://backend/ |
/ |
http://backend |
/hello |
http://nginx/hello |
http://backend/hello |
/ |
http://backend |
/hello/world |
http://nginx/hello/world |
http://backend/hello/world |
location |
proxy_pass |
path |
nginx url |
backend url |
/a |
http://backend |
/a |
http://nginx/a |
http://backend/a |
/a |
http://backend |
/ab |
http://nginx/ab |
http://backend/ab |
/a |
http://backend |
/a/ |
http://nginx/a/ |
http://backend/a/ |
/a |
http://backend |
/a/b |
http://nginx/a/b |
http://backend/a/b |
/a |
http://backend |
/a/b/ |
http://nginx/a/b/ |
http://backend/a/b/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/b/ |
http://backend |
/b/ |
http://nginx/b/ |
http://backend/b/ |
/b/ |
http://backend |
/b/a |
http://nginx/b/a |
http://backend/b/a |
/b/ |
http://backend |
/b/a/ |
http://nginx/b/a/ |
http://backend/b/a/ |
2. proxy_pass 带路径时
这种情况下,相当于下面的公式:
backend url = proxy_pass + (path - location)
path 部分减去匹配的 location 部分后,剩余内容追加到 proxy_pass 上去请求。
如果不是为了/u
去匹配 /uv
这种路径,使用 /v/
去匹配 /v/w
这种更准确,不会出现 //
这种情况。
location |
proxy_pass |
path |
nginx url |
backend url |
/u |
http://backend/ |
/u |
http://nginx/u |
http://backend/ |
/u |
http://backend/ |
/uv |
http://nginx/uv |
http://backend/v |
/u |
http://backend/ |
/u/ |
http://nginx/u/ |
http://backend// |
/u |
http://backend/ |
/u/v |
http://nginx/u/v |
http://backend//v |
/u |
http://backend/ |
/u/v/ |
http://nginx/u/v/ |
http://backend//v/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/v/ |
http://backend/ |
/v/ |
http://nginx/v/ |
http://backend/ |
/v/ |
http://backend/ |
/v/w |
http://nginx/v/w |
http://backend/w |
/v/ |
http://backend/ |
/v/w/ |
http://nginx/v/w/ |
http://backend/w/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/c |
http://backend/c |
/c |
http://nginx/c |
http://backend/c |
/c |
http://backend/c |
/cd |
http://nginx/cd |
http://backend/cd |
/c |
http://backend/c |
/c/ |
http://nginx/c/ |
http://backend/c/ |
/c |
http://backend/c |
/c/d |
http://nginx/c/d |
http://backend/c/d |
/c |
http://backend/c |
/c/d/ |
http://nginx/c/d/ |
http://backend/c/d/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/d/ |
http://backend/d |
/d/ |
http://nginx/d/ |
http://backend/d |
/d/ |
http://backend/d |
/d/e |
http://nginx/d/e |
http://backend/de |
/d/ |
http://backend/d |
/d/e/ |
http://nginx/d/e/ |
http://backend/de/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/e |
http://backend/e/ |
/e |
http://nginx/e |
http://backend/e/ |
/e |
http://backend/e/ |
/ef |
http://nginx/ef |
http://backend/e/f |
/e |
http://backend/e/ |
/e/ |
http://nginx/e/ |
http://backend/e// |
/e |
http://backend/e/ |
/e/f |
http://nginx/e/f |
http://backend/e//f |
/e |
http://backend/e/ |
/e/f/ |
http://nginx/e/f/ |
http://backend/e//f/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/f/ |
http://backend/f/ |
/f/ |
http://nginx/f/ |
http://backend/f/ |
/f/ |
http://backend/f/ |
/f/g |
http://nginx/f/g |
http://backend/f/g |
/f/ |
http://backend/f/ |
/f/g/ |
http://nginx/f/g/ |
http://backend/f/g/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/g |
http://backend/m |
/g |
http://nginx/g |
http://backend/m |
/g |
http://backend/m |
/gh |
http://nginx/gh |
http://backend/mh |
/g |
http://backend/m |
/g/ |
http://nginx/g/ |
http://backend/m/ |
/g |
http://backend/m |
/g/h |
http://nginx/g/h |
http://backend/m/h |
/g |
http://backend/m |
/g/h/ |
http://nginx/g/h/ |
http://backend/m/h/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/h/ |
http://backend/n |
/h/ |
http://nginx/h/ |
http://backend/n |
/h/ |
http://backend/n |
/h/i |
http://nginx/h/i |
http://backend/ni |
/h/ |
http://backend/n |
/h/i/ |
http://nginx/h/i/ |
http://backend/ni/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/i |
http://backend/x/ |
/i |
http://nginx/i |
http://backend/x/ |
/i |
http://backend/x/ |
/ij |
http://nginx/ij |
http://backend/x/j |
/i |
http://backend/x/ |
/i/ |
http://nginx/i/ |
http://backend/x// |
/i |
http://backend/x/ |
/i/j |
http://nginx/i/j |
http://backend/x//j |
/i |
http://backend/x/ |
/i/j/ |
http://nginx/i/j/ |
http://backend/x//j/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/j/ |
http://backend/y/ |
/j/ |
http://nginx/j/ |
http://backend/y/ |
/j/ |
http://backend/y/ |
/j/k |
http://nginx/j/k |
http://backend/y/k |
/j/ |
http://backend/y/ |
/j/k/ |
http://nginx/j/k/ |
http://backend/y/k/ |
location |
proxy_pass |
path |
nginx url |
backend url |
/ws/ |
http://backend/spring/ws/ |
/ws/qrlogin |
http://nginx/ws/qrlogin |
http://backend/spring/ws/qrlogin |
/ws/ |
http://backend/spring/ws/ |
/ws/stomp |
http://nginx/ws/stomp |
http://backend/spring/ws/stomp |
参考文档:
- 英文 - ngx_http_proxy_module.html#proxy_pass
- 中文 - ngx_http_proxy_module#proxy_pass