解读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实现TCP和UDP代理的方法步骤

    Nginx实现TCP和UDP代理的方法步骤

    Nginx 1.9.13 及以上版本支持TCP/UDP代理功能,通过配置监听端口、后端服务器地址等参数,实现客户端请求的转发和响应的返回,下面就来介绍一下如何实现,感兴趣的可以了解一下
    2024-12-12
  • nginx添加ssl模块的方法教程

    nginx添加ssl模块的方法教程

    这篇文章主要给大家介绍了关于nginx添加ssl模块的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习许吧。
    2017-12-12
  • Nginx上传文件出现“ 413 (499 502 404) Request Entity Too Large错误解决

    Nginx上传文件出现“ 413 (499 502 404) Requ

    HTTP 413 Request Entity Too Large错误常常出现在客户端发送的请求体超过服务器允许的大小限制时,本文主要介绍了Nginx上传文件出现“ 413 (499 502 404) Request Entity Too Large错误解决,感兴趣的可以了解一下
    2024-07-07
  • Nginx双机热备的实现步骤

    Nginx双机热备的实现步骤

    本文主要介绍了Nginx双机热备的实现步骤,是国内企业中最为普遍的一种高可用方案,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • 制作能在nginx和IIS中使用的ssl证书

    制作能在nginx和IIS中使用的ssl证书

    现在的后端开发,动不动就是需要https,或者说是需要ssl证书,既然没有正版的证书,那么我们只能自己制作ssl的证书了。 证书的制作采用的是openssl工具,如果没有,可以自行安装下,因为笔者是在linux(ubuntu)下制作,所以一般是使用包管理工具(apt)安装好了的
    2021-06-06
  • Nginx中配置WebSocket代理的详细步骤

    Nginx中配置WebSocket代理的详细步骤

    Nginx 可以配置为 WebSocket 代理,将 WebSocket 连接从客户端转发到后端服务器,以下是如何在 Nginx 中配置 WebSocket 代理的详细步骤和示例配置,需要的朋友可以参考下
    2025-02-02
  • Nginx丢弃http包体处理实例详解

    Nginx丢弃http包体处理实例详解

    这篇文章主要介绍了Nginx丢弃http包体处理实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • nginx worker进程循环的实现

    nginx worker进程循环的实现

    这篇文章主要介绍了nginx worker进程循环的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • nginx编译安装出现的常见错误及解决方法

    nginx编译安装出现的常见错误及解决方法

    这篇文章给大家介绍了nginx在编译安装过程中容易出现的常见错误以及解决方法,文中有详细的代码讲解,对我们的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-08-08
  • nginx fair负载均衡方式使用详解

    nginx fair负载均衡方式使用详解

    本文主要介绍了nginx fair负载均衡方式使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08

最新评论