• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

nginx反向代理location和proxy_pass的映射关系

武飞扬头像
isea533
帮助1

配置nginx反向代理时,总是需要尝试多次才能配置成功,通过本文做个记录,方便以后查看。

1. proxy_pass 只有主机地址时

只有主机地址的意思是,proxy_pass 值为 http://host:port 这种形式,url部分没有 //xxx 等.
对应到本文示例就是 http://backend

这种情况下,相当于下面的公式:

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

参考文档:

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfkjkk
系列文章
更多 icon
同类精品
更多 icon
继续加载