在日常的web网站部署中,经常会用到nginx的proxy_pass反向代理,有一个配置需要弄清楚:配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走(这样配置在Nginx反向代理+负载均衡简单实现(http方式)也提到过)。
下面举个小实例说明下:
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/

===================================如下一简单配置示例=============================

只有当访问http://www.kevin.com/los/.....的时候才代理负载到http://192.168.10.24:50006/los/.... 和 http://192.168.10.25:50006/los/....上,
也就是说访问www.kevin.com域名, 只有在后面匹配los路径时才代理负载到192.168.10.24/25的50006端口的los路径下, 除此之外, 访问域名
www.kevin.com 匹配其他任何路径(包括/, 即http://www.kevin.com) 时都跳转到一个错误页面: [root@external-lb02 vhosts]# cat 80-www.kevin.com.conf
upstream web-inf-80 {
ip_hash;
server 192.168.10.24:50006 max_fails=3 fail_timeout=15s;
server 192.168.10.25:50006 max_fails=3 fail_timeout=15s;
} server {
listen 80;
server_name www.kevin.com; access_log /data/nginx/logs/www.kevin.com-access.log main;
error_log /data/nginx/logs/www.kevin.com-error.log; location / {
root /opt/web-inf;
index index.php index.html index.htm;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /opt/web-inf;
} error_page 404 /404.html;
location = /404.html {
root /opt/web-inf;
} location /los/ {
proxy_pass http://web-inf-80;
proxy_set_header Host $host;
#proxy_redirect http://web-inf/ http://www.kevin.com/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
}
} 错误页面设置:
[root@external-lb02 vhosts]# cd /opt/web-inf/
[root@external-lb02 web-inf]# ls
404.html 50x.html error.html index.html
[root@external-lb02 web-inf]# cat error.html
this is error page!
[root@external-lb02 web-inf]# cat index.html
this is error page!
[root@external-lb02 web-inf]# cat 404.html
this is error page!
[root@external-lb02 web-inf]# cat 50x.html
this is error page!

proxy_pass反向代理配置中url后面加不加/的说明的更多相关文章

  1. nginx反向代理+缓存开启+url重写+负载均衡(带健康探测)的部署记录

    在日常运维工作中,运维人员会时常使用到nginx的反向代理,负载均衡以及缓存等功能来优化web服务性能. 废话不多说,下面对测试环境下的nginx反向代理+缓存开启+url重写+负载均衡(带健康探测) ...

  2. nginx反向代理配置

    最近在项目中使用nginx反向代理,根据不同的请求路径,将请求分发到不同服务.下面的示例主要完成如下功能 /prod/路径的请求分发到prod服务 /test/路径的请求分发到test服务 创建文件夹 ...

  3. Nginx 部署、反向代理配置、负载均衡

    Nginx 部署.反向代理配置.负载均衡 最近我们的angular项目部署,我们采用的的是Nginx,下面对Nginx做一个简单的介绍. 为什么选择Nginx 轻:相比于Apache,同样的web服务 ...

  4. nginx反向代理配置及优化

    nginx反向代理配置及优化前言: 由于服务器apache抗不住目前的并发.加上前端squid配置后,问题依然无法解决.而页面程序大部分是动态.无法使用fastcgi来处理.因此想使用nginx做为反 ...

  5. IIS与JIRA的反向代理配置

    JIRA配置修改 JIRA与IIS ARR的集成,除了上篇(Visual SVN IIS反向代理设置)中讲到的基本的ARR配置之外,还需要在JIRA安装目录的conf\server.xml文件中做一个 ...

  6. apache 反向代理配置

    配置前资料检查: 1.可以使用的apache 安装apache服务:打开cmd , 在apache的bin目录下执行以下命令 httpd -k install -n apache2.2    其中&q ...

  7. iis7.5做反向代理配置方法实例图文教程

    网络上好多开场的文章就说了好多的原理之类的这里我们直接开始配置.不过也要简单说下win下配置反向代理只有IIS7以上的版本才可以实现这个功能,在这里我们使用WINDOWS2008 R2来做为测试 20 ...

  8. angular反向代理配置

    Angular-cli 是基于webpack 的一套针对提升angular开发体验的命令行工具. 开发vue的时候,基于webpack的时候当时配置一个反向代理以完全实现前后端分离的体验,既然webp ...

  9. nginx反向代理配置实例分享

    nginx反向代理配置一例. 配置内容如下: user www www; worker_processes 8; error_log /usr/local/webserver/nginx/logs/n ...

随机推荐

  1. 如何用vmware workstation来做虚拟化实验

    前言 以前做用vmare只是简单的实验,但是随着现在虚拟化的兴起,我们的开始要开始虚拟化的实验了. 我们看到有些windows 2012的书上面说用hyper-v来实验,但是hyper-v只能做一些列 ...

  2. VS2015 无法启动 IIS Express Web 服务器 解决方案

    VS2015 IIS Express 无法启动Web 解决方案 [亲测已成功] 1.我的电脑—管理—事件查看器—Windows日志—应用程序: 详细信息会提示你:[模块 DLL C:\Program ...

  3. tkinter学习系列(三)之Label控件

    目录 目录 前言 (一)基本用法和可选属性 ==1.基本用法== ==2.可选属性== (二)属性的具体使用 ==1.常用属性== ==2.边距与文本对齐方式== ==案例一== ==案例一的效果== ...

  4. January 27th, 2018 Week 04th Saturday

    How long is forever? Sometimes, just one second. 永远有多久?有时候只是一秒. Just one second can make your life t ...

  5. Java中选择排序,冒泡排序,插入排序,快速排序

    一:冒泡法排序  //冒泡排序 注:从小到大排   //特点:效率低,实现简单  //思想:每一趟将待排序序列中最大元素移到最后,剩下的为新的待排序序列,重复上述步骤直到排完所有元素. 这只是冒泡排序 ...

  6. 控件布局_TableLayout

    <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android=" ...

  7. Usaco 2019 Jan Platinum

    Usaco 2019 Jan Platinum 要不是昨天老师给我们考了这套题,我都不知道usaco还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...

  8. web安全之攻击

    转自 知乎https://www.zhihu.com/question/22953267 作者:潘良虎链接:https://www.zhihu.com/question/22953267/answer ...

  9. Redis学习笔记--Redis数据过期策略详解==转

    本文对Redis的过期机制简单的讲解一下 讲解之前我们先抛出一个问题,我们知道很多时候服务器经常会用到redis作为缓存,有很多数据都是临时缓存一下,可能用过之后很久都不会再用到了(比如暂存sessi ...

  10. Git使用—第一讲:初识版本控制工具

    几乎所有出色的项目都不是一个人完成的,而是由一个团队共同合作开发完成的,这个时候多人之间的代码同步问题就显得异常重要了,因此版本控制工具也就应运而生了.常见的版本控制工具主要有SVN和Git,接下来要 ...