nginx location proxy pass

Posted whitesky_root

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx location proxy pass相关的知识,希望对你有一定的参考价值。

 

nginx:
192.168.1.23作为nginx反向代理机器
目标机器192.168.1.5上部署一个8090端口的nginx

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location  /proxy/ {
          proxy_pass http://192.168.1.5:8090/;
}
}

访问http://192.168.1.23/proxy/就会被代理到http://192.168.1.5:8090/, 浏览器的url是http://192.168.1.23/proxy/不变,
目标机器上无需存在proxy目录(curl http://192.168.1.23/proxy 可以发现是302,301重定向)


如果:proxy_pass配置的url后面不加"/"
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location  /proxy/ {
          proxy_pass http://192.168.1.5:8090;
}
}

那么访问http://192.168.1.23/proxy或http://192.168.1.23/proxy/,都会失败!
这样配置后,访问http://192.168.1.23/proxy/就会被反向代理到http://192.168.1.5:8090/proxy/,目标机必须有proxy目录 

快速记忆:proxypass最后如果带 / ,则代理到目标机就没有location后面的目录,  不带 /  , 到目标机后携带location后面的目录,一起带过去了

 

以上是关于nginx location proxy pass的主要内容,如果未能解决你的问题,请参考以下文章

使用 nginx proxy_pass 修改 Location 标头

Nginx学习-实例:location+proxy_pass配置中左斜杠彻底弄清

nginx rewrite proxy_pass location 的理解

Nginx下的location,upstream,rewrite 和 proxy_pass使用总计大全

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

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