当前位置:首页 > 技术文章 > 正文内容

nginx反向代理斜杠“/”问题总是记不住?

arlanguage4个月前 (12-24)技术文章32

工作中经常会使用nginx 配置反向代理,代理末尾的斜杠配置组合大体上可以分为8种,所以总是忘记 location 和 proxy_pass 最后面的斜杠会对代理结果有怎样的影响。今天我们就来试试找个统一的规则。

server {
    listen 8000;
    server_name localhost;

    #配置反向代理
    location /test/ {
        proxy_pass http://127.0.0.1:9000/api/;
    }
}

我们简单配置个反向代理的 conf,监听端口为8000,代理端口为 9000,下面分别罗列出 location 和proxy_pass 斜杠的8类组合,以及代理的结果。

第一种斜杠配置

#配置1
location /test/ {
  	proxy_pass http://127.0.0.1:9000/api/;
}

模拟请求

curl http://localhost:8000/test/user/query

由于我没有开启端口为9000的服务,nginx会返回502,所以来看下nginx的 error.log日志,截取一部分如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/api/user/query"

其中,upstream 后面就是代理的结果,对比模拟请求,可以发现除了 ip:port 被替换,还少了个 "test",我们大胆推测一下,nginx 监听到模拟请求后,判断其和 "/test/" 匹配,截取了 "http://localhost:8000/test/" ,剩余的 "user/query" 拼接到 proxy_pass 的内容后面,就得到了 "http://127.0.0.1:9000/api/user/query"

第二种斜杠配置

#配置2
location /test {
    proxy_pass http://127.0.0.1:9000/api;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/api/user/query"

这里截取了 "http://localhost:8000/test" ,剩余 "/user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000/api/user/query",符合推测。

第三种斜杠配置

#配置3
location /test {
    proxy_pass http://127.0.0.1:9000/api/;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/api//user/query"

这里截取了 "http://localhost:8000/test" ,剩余 "/user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000/api//user/query",同样符合推测。

第四种斜杠配置

#配置4
location /test/ {
    proxy_pass http://127.0.0.1:9000/api;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/apiuser/query"

这里截取了 "http://localhost:8000/test/" ,剩余 "user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000/apiuser/query",同样符合推测。

第五种斜杠配置

#配置5
location /test/ {
    proxy_pass http://127.0.0.1:9000/;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/user/query"

这里截取了 "http://localhost:8000/test/" ,剩余 "user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000/user/query",同样符合推测。

第六种斜杠配置

#配置6
location /test {
    proxy_pass http://127.0.0.1:9000/;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000//user/query"

这里截取了 "http://localhost:8000/test" ,剩余 "/user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000//user/query",符合推测。

第七种斜杠配置

#配置7
location /test {
    proxy_pass http://127.0.0.1:9000;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/test/user/query"

这里截取 "http://localhost:8000/test" ,剩余 "/user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000/user/query",很遗憾不符合推测,但是比实际结果刚好少了 location 后面的内容 "/test"。

第八种斜杠配置

#配置8
Location /test/ {
    proxy_pass http://127.0.0.1:9000;
}

模拟请求

curl http://localhost:8000/test/user/query

error.log日志截取如下:

request: "GET /test/user/query HTTP/1.1", upstream: "http://127.0.0.1:9000/test/user/query"

这里截取 "http://localhost:8000/test/" ,剩余 "user/query" 拼接到 proxy_pass 的内容后面,得到 "http://127.0.0.1:9000user/query",同样的也是比实际结果刚好少了location后面的内容 "/test/"。

结果分析

经过分析所有情况,我们可以得到以下结论:

当proxy_pass 后面的内容不是以端口号结尾,即端口号后面有"/**"时,其实就是将请求的地址,截取掉 location 后面的内容,将剩余内容拼接到proxy_pass代理地址上;

当proxy_pass 后面的内容以端口号结尾,即端口号后面什么都没有,可以视为默认拼接了 location 的内容。那么配置7,8 就等同如下配置,再套用上面的结论就可以等到正确的结果。

#配置7
location /test {
    proxy_pass http://127.0.0.1:9000/test;
}

#配置8
Location /test/ {
    proxy_pass http://127.0.0.1:9000/test/;
}

不知道 nginx 的源码关于这里的逻辑是怎样处理的,但是有上面的结论以后再配置反向代理应该不会因为末尾的斜杠疑惑了。

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/417.html

分享给朋友:

“nginx反向代理斜杠“/”问题总是记不住?” 的相关文章

haproxy负载均衡入门到转行

haproxy概述haproxy是一款开源的高性能的反向代理或者说是负载均衡服务软件之一,支持双机热备,虚拟主机基于TCP/HTTP应用代理,具有图形界面等功能。其配置简单,而且拥有很好的对服务器节点的健康检查功能(相当于keepalived健康检查),当其代理的后端服务器出现故障时,haproxy...

利用Nginx正向代理实现局域网电脑访问外网

引言在网络环境中,有时候我们需要让局域网内的电脑访问外网,但是由于网络策略或其他原因,直接访问外网是不可行的。这时候,可以借助 Nginx 来搭建一个正向代理服务器,实现局域网内电脑通过 Nginx 转发访问外网的需求。在工作中我遇到了一个类似的情况:在公司网络中,由于管理要求,局域网内的电脑不能直...

windows 搭建php运行环境(2024年)

由于需要运行一套cms系统,需要运行php环境,现在开始记录搭建步骤:1、分别下载nginx和php包,然后解压到自己喜欢目录下面2、这里我们使用nginx的反向代理模式的FastCGI server,修改nginx.conf,然后启动或重启nginx。location ~ \.php$ {...

压测nginx出现的问题分析

压测nginx出现no live upstreams while connecting to upstream的问题分析基础环境版本信息Centos 7.1nginx version: openresty/1.13.6.2nginx配置信息stream {   ...

405状态码Nginx解决办法

最近由于业务系统并发量比较大,所以在已有负载均衡的基础上,我们做了Nginx动静分离,但是系统访问后报错,经过F12(浏览器开发者模式)和Nginx日志分析,发现请求状态变成了405,故百度解释如下:问题原因: 请求的方式(get、post、delete)方法与后台规定的方式不符合。比如: 后台方...

Nginx实战-监控nginx.conf配置文件,配置文件修改自动重启nginx

1.1 实现目标在学习或者进行nginx测试的时候,耗费在 nginx -s reload/stop 上的命令时间很多,修改任意内容都需要重新启动或者停止启动,基本上状态就是在下面5个状态间来回切换vim nginx.conf修改nginx.conf保存nginx.conf重启nginx刷新浏览器....