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

 更新时间:2024年01月26日 14:21:24   作者:isea533  
这篇文章主要介绍了解读nginx反向代理location和proxy_pass的映射关系,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

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

1. proxy_pass 只有主机地址时

只有主机地址的意思是,proxy_pass 值为 http://host:port 这种形式,url部分没有 //xxx 等.

对应到本文示例就是 http://backend

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

backend url = proxy_pass + path

这种情况下,请求的 path 部分会直接追加到 proxy_pass 地址后,相当于把nginx地址一对一的映射到了后端地址,这种配置方式理解最简单。

locationproxy_passpathnginx urlbackend url
/http://backend/http://nginx/http://backend/
/http://backend/hellohttp://nginx/hellohttp://backend/hello
/http://backend/hello/worldhttp://nginx/hello/worldhttp://backend/hello/world
locationproxy_passpathnginx urlbackend url
/ahttp://backend/ahttp://nginx/ahttp://backend/a
/ahttp://backend/abhttp://nginx/abhttp://backend/ab
/ahttp://backend/a/http://nginx/a/http://backend/a/
/ahttp://backend/a/bhttp://nginx/a/bhttp://backend/a/b
/ahttp://backend/a/b/http://nginx/a/b/http://backend/a/b/
locationproxy_passpathnginx urlbackend url
/b/http://backend/b/http://nginx/b/http://backend/b/
/b/http://backend/b/ahttp://nginx/b/ahttp://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 这种更准确,不会出现 // 这种情况。

locationproxy_passpathnginx urlbackend url
/uhttp://backend//uhttp://nginx/uhttp://backend/
/uhttp://backend//uvhttp://nginx/uvhttp://backend/v
/uhttp://backend//u/http://nginx/u/http://backend//
/uhttp://backend//u/vhttp://nginx/u/vhttp://backend//v
/uhttp://backend//u/v/http://nginx/u/v/http://backend//v/
locationproxy_passpathnginx urlbackend url
/v/http://backend//v/http://nginx/v/http://backend/
/v/http://backend//v/whttp://nginx/v/whttp://backend/w
/v/http://backend//v/w/http://nginx/v/w/http://backend/w/
locationproxy_passpathnginx urlbackend url
/chttp://backend/c/chttp://nginx/chttp://backend/c
/chttp://backend/c/cdhttp://nginx/cdhttp://backend/cd
/chttp://backend/c/c/http://nginx/c/http://backend/c/
/chttp://backend/c/c/dhttp://nginx/c/dhttp://backend/c/d
/chttp://backend/c/c/d/http://nginx/c/d/http://backend/c/d/
locationproxy_passpathnginx urlbackend url
/d/http://backend/d/d/http://nginx/d/http://backend/d
/d/http://backend/d/d/ehttp://nginx/d/ehttp://backend/de
/d/http://backend/d/d/e/http://nginx/d/e/http://backend/de/
locationproxy_passpathnginx urlbackend url
/ehttp://backend/e//ehttp://nginx/ehttp://backend/e/
/ehttp://backend/e//efhttp://nginx/efhttp://backend/e/f
/ehttp://backend/e//e/http://nginx/e/http://backend/e//
/ehttp://backend/e//e/fhttp://nginx/e/fhttp://backend/e//f
/ehttp://backend/e//e/f/http://nginx/e/f/http://backend/e//f/
locationproxy_passpathnginx urlbackend url
/f/http://backend/f//f/http://nginx/f/http://backend/f/
/f/http://backend/f//f/ghttp://nginx/f/ghttp://backend/f/g
/f/http://backend/f//f/g/http://nginx/f/g/http://backend/f/g/
locationproxy_passpathnginx urlbackend url
/ghttp://backend/m/ghttp://nginx/ghttp://backend/m
/ghttp://backend/m/ghhttp://nginx/ghhttp://backend/mh
/ghttp://backend/m/g/http://nginx/g/http://backend/m/
/ghttp://backend/m/g/hhttp://nginx/g/hhttp://backend/m/h
/ghttp://backend/m/g/h/http://nginx/g/h/http://backend/m/h/
locationproxy_passpathnginx urlbackend url
/h/http://backend/n/h/http://nginx/h/http://backend/n
/h/http://backend/n/h/ihttp://nginx/h/ihttp://backend/ni
/h/http://backend/n/h/i/http://nginx/h/i/http://backend/ni/
locationproxy_passpathnginx urlbackend url
/ihttp://backend/x//ihttp://nginx/ihttp://backend/x/
/ihttp://backend/x//ijhttp://nginx/ijhttp://backend/x/j
/ihttp://backend/x//i/http://nginx/i/http://backend/x//
/ihttp://backend/x//i/jhttp://nginx/i/jhttp://backend/x//j
/ihttp://backend/x//i/j/http://nginx/i/j/http://backend/x//j/
locationproxy_passpathnginx urlbackend url
/j/http://backend/y//j/http://nginx/j/http://backend/y/
/j/http://backend/y//j/khttp://nginx/j/khttp://backend/y/k
/j/http://backend/y//j/k/http://nginx/j/k/http://backend/y/k/
locationproxy_passpathnginx urlbackend url
/ws/http://backend/spring/ws//ws/qrloginhttp://nginx/ws/qrloginhttp://backend/spring/ws/qrlogin
/ws/http://backend/spring/ws//ws/stomphttp://nginx/ws/stomphttp://backend/spring/ws/stomp

参考文档:

英文 - ngx_http_proxy_module.html#proxy_pass

中文 - ngx_http_proxy_module#proxy_pass

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Nginx解决history模式下页面刷新404问题示例

    Nginx解决history模式下页面刷新404问题示例

    这篇文章主要为大家介绍了Nginx解决history模式下页面刷新404问题示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • 基于Nginx的衍生版服务器Tengine简介

    基于Nginx的衍生版服务器Tengine简介

    这篇文章主要介绍了基于Nginx的衍生版服务器Tengine简介,本文讲解了Nginx的特性、Tengine的特性、架构和扩展性等内容,需要的朋友可以参考下
    2015-03-03
  • 详解使用ChatGPT解决Nginx反向代理的问题

    详解使用ChatGPT解决Nginx反向代理的问题

    这篇文章主要为大家介绍了使用ChatGPT解决Nginx反向代理的问题详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • 使用Nginx和pm2部署Next.js项目

    使用Nginx和pm2部署Next.js项目

    本文主要介绍了使用Nginx和pm2部署Next.js项目,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Nginx安装后常用功能配置基础篇

    Nginx安装后常用功能配置基础篇

    这篇文章主要介绍了Nginx安装后常用的功能配置,为了在使用中更高效简洁,Nginx安装后通常会进行一些常用的配置,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-03-03
  • nginx处理http请求实例详解

    nginx处理http请求实例详解

    这篇文章主要介绍了nginx处理http请求实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • Nginx出现403 Forbidden的几种简单解决方式

    Nginx出现403 Forbidden的几种简单解决方式

    这篇文章主要介绍了Nginx出现403 Forbidden的几种解决思路,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • 配置nginx保证frps服务器与web共用80端口的方法

    配置nginx保证frps服务器与web共用80端口的方法

    这篇文章主要介绍了frps服务端与nginx可共用80端口的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • centos7编译安装nginx的方法步骤

    centos7编译安装nginx的方法步骤

    这篇文章主要介绍了centos7编译安装nginx的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Nginx路径重写方式

    Nginx路径重写方式

    这篇文章主要介绍了Nginx路径重写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论