前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >proxy_pass反向代理配置中url后面加不加/的说明

proxy_pass反向代理配置中url后面加不加/的说明

作者头像
洗尽了浮华
发布2018-01-23 14:48:10
2.2K0
发布2018-01-23 14:48:10
举报
文章被收录于专栏:散尽浮华散尽浮华

在日常的web网站部署中,经常会用到nginx的proxy_pass反向代理,有一个配置需要弄清楚:配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走(这样配置在Nginx反向代理+负载均衡简单实现(http方式)也提到过)。 下面举个小实例说明下: centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库

代码语言:javascript
复制
1)使用yum安装nginx需要包括Nginx的库,安装Nginx的库
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
 
2)使用下面命令安装nginx
[root@localhost ~]# yum install nginx
 
3)nginx配置
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}

[root@localhost conf.d]# cat /var/www/html/index.html 
this is page of test!!!!
 
4)启动Nginx
[root@localhost ~]# service nginx start     //或者使用 systemctl start nginx.service
 
5)测试访问(103.110.186.23是192.168.1.23机器的外网ip)
[root@localhost conf.d]# curl http://192.168.1.23
this is page of test!!!!

--------------------------看看下面几种情况:分别用http://192.168.1.23/proxy/index.html进行访问测试-----------------

为了方便测试,先在另一台机器192.168.1.5上部署一个8090端口的nginx,配置如下:

代码语言:javascript
复制
[root@bastion-IDC ~]# cat /usr/local/nginx/conf/vhosts/haha.conf 
server {
listen 8090;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
[root@bastion-IDC ~]# cat /var/www/html/index.html 
this is 192.168.1.5
[root@bastion-IDC ~]# /usr/local/nginx/sbin/nginx -s reload

测试访问(103.110.186.5是192.168.1.5的外网ip):
[root@bastion-IDC ~]# curl http://192.168.1.5:8090
this is 192.168.1.5

192.168.1.23作为nginx反向代理机器,nginx配置如下: 1)第一种情况:

代码语言:javascript
复制
[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/。p匹配的proxy目录不需要存在根目录/var/www/html里面 注意,终端里如果访问http://192.168.1.23/proxy(即后面不带"/"),则会访问失败!因为proxy_pass配置的url后面加了"/"

代码语言:javascript
复制
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
this is 192.168.1.5
[root@localhost conf.d]# curl http://192.168.1.23/proxy
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

页面访问http://103.110.186.23/proxy的时候,会自动加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的结果

2)第二种情况,proxy_pass配置的url后面不加"/"

代码语言:javascript
复制
[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;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service

那么访问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/

3)第三种情况

代码语言:javascript
复制
[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/haha/;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
192.168.1.5  haha-index.html

这样配置的话,访问http://103.110.186.23/proxy代理到http://192.168.1.5:8090/haha/

image.png
image.png

4)第四种情况:相对于第三种配置的url不加"/"

代码语言:javascript
复制
[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/haha;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5   hahaindex.html

上面配置后,访问http://192.168.1.23/proxy/index.html就会被代理到http://192.168.1.5:8090/hahaindex.html
同理,访问http://192.168.1.23/proxy/test.html就会被代理到http://192.168.1.5:8090/hahatest.html
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5   hahaindex.html

注意,这种情况下,不能直接访问http://192.168.1.23/proxy/,后面就算是默认的index.html文件也要跟上,否则访问失败!

------------------------------------------------------------------------------------- 上面四种方式都是匹配的path路径后面加"/",下面说下path路径后面不带"/"的情况:

1)第一种情况,proxy_pass后面url带"/":

代码语言:javascript
复制
[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/;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service

2)第二种情况,proxy_pass后面url不带"/"

代码语言:javascript
复制
[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;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service
[root@localhost conf.d]# 

这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/

3)第三种情况

代码语言:javascript
复制
[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/haha/;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service

这样配置的话,访问http://103.110.186.23/proxy会自动加上"/”(即变成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/

4)第四种情况:相对于第三种配置的url不加"/"

代码语言:javascript
复制
[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/haha;
}
}
[root@localhost conf.d]# service nginx restart
Redirecting to /bin/systemctl restart  nginx.service

这样配置的话,访问http://103.110.186.23/proxy,和第三种结果一样,同样被代理到http://192.168.1.5:8090/haha/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档