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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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

1

2

3

4

5

6

7

8

9

10

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

只有当访问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!

原文链接:https://www.cnblogs.com/kevingrace/p/6566119.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值