nginx proxy_pass反向代理配置中url后加不加/的区别介绍

 更新时间:2017年11月03日 12:04:36   作者:散尽浮华  
这篇文章主要给大家介绍了关于nginx proxy_pass反向代理配置中url后加不加/的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

nginx作为web服务器一个重要的功能就是反向代理。nginx反向代理的指令不需要新增额外的模块,默认自带proxy_pass指令,只需要修改配置文件就可以实现反向代理。

而在日常的web网站部署中,经常会用到nginx的proxy_pass反向代理,有一个配置需要弄清楚:配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走(这样配置可以参考这篇文章)。

下面举个小实例说明下:

centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库

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,配置如下:

[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)第一种情况:

[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后面加了"/"

[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后面不加"/"

[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)第三种情况

[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/

4)第四种情况:相对于第三种配置的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/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带"/":

[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不带"/"

[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)第三种情况

[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不加"/"

[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/

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • nginx实现数据库端口转发

    nginx实现数据库端口转发

    本文主要介绍了nginx实现数据库端口转发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Windows下Nginx安装配置教程

    Windows下Nginx安装配置教程

    这篇文章主要为大家介绍了Windows下Nginx安装配置的详细教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Nginx+Tomcat+Https 服务器负载均衡配置实践方案详解

    Nginx+Tomcat+Https 服务器负载均衡配置实践方案详解

    这篇文章主要介绍了Nginx+Tomcat+Https 服务器负载均衡配置实践方案的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Nginx 禁用静态文件缓存的配置方法

    Nginx 禁用静态文件缓存的配置方法

    禁用缓存可能会导致性能下降,因为每次请求都需要从后端服务器获取文件,因此,你需要根据具体情况权衡利弊并做出决策,这篇文章给大家介绍Nginx 禁用静态文件缓存的方法,感兴趣的朋友一起看看吧
    2024-02-02
  • nginx代理部署Vue刷新页面404的问题解决

    nginx代理部署Vue刷新页面404的问题解决

    在上线vue开发的前端网页部署在服务器上后,刷新页面显示404,本文就来介绍一下nginx代理部署Vue刷新页面404的问题解决,感兴趣的可以了解一下
    2023-12-12
  • Nginx map 实现时间格式转换的方法

    Nginx map 实现时间格式转换的方法

    最近我们需要把 Nginx 的日志接入到自研的日志采集平台上,但是这个平台只支持 JSON 格式,所以需要把 Nginx 日志格式改成 JSON 格式,这篇文章主要介绍了Nginx map 实现时间格式转换,需要的朋友可以参考下
    2023-09-09
  • Nginx下301重定向域名的方法小结

    Nginx下301重定向域名的方法小结

    在网站建设中需要网页重定向的情况很多:如网页目录结构变动,网页重命名、网页的扩展名改变、网站域名改变等。
    2010-08-08
  • nginx配置支持https的示例代码

    nginx配置支持https的示例代码

    这篇文章主要介绍了nginx配置支持https的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • 为Nginx添加SPDY功能

    为Nginx添加SPDY功能

    我也开始尝试着给自己的论坛加上SPDY协议,WEB服务器本人选择的是nginx,在过去,Nginx并没有内置SPDY协议,需要打开的话还要下载开发版然后手动编译,很不方便
    2014-12-12
  • 详解Nginx 工作原理

    详解Nginx 工作原理

    这篇文章主要介绍了Nginx 工作原理,帮助大家更好的理解和学习nginx,感兴趣的朋友可以了解下
    2020-10-10

最新评论