1. 程式人生 > >proxy_pass反向代理配置中url後面加不加/的說明

proxy_pass反向代理配置中url後面加不加/的說明

在日常的web網站部署中,經常會用到nginx的proxy_pass反向代理,有一個配置需要弄清楚:配置proxy_pass時,當在後面的url加上了/,相當於是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分也給代理走。 下面舉個小例項說明下: 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的庫

[[email protected] ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2)使用下面命令安裝nginx

[[email protected] ~]# yum install nginx

3)nginx配置

[[email protected] ~]# cd /etc/nginx/conf.d/

[[email protected] conf.d]# cat test.conf

server {

listen 80;

server_name localhost;

location / {

root /var/www/html;

index index.html;

}

}

[[email protected] conf.d]# cat /var/www/html/index.html

this is page of test!!!!

4)啟動Nginx

[[email protected] ~]# service nginx start     //或者使用 systemctl start nginx.service

5)測試訪問(103.110.186.23是192.168.1.23機器的外網ip)

[[email protected] 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

[[email protected] ~]# cat /usr/local/nginx/conf/vhosts/haha.conf

server {

listen 8090;

server_name localhost;

location / {

root /var/www/html;

index index.html;

}

}

[[email protected] ~]# cat /var/www/html/index.html

this is 192.168.1.5

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

測試訪問(103.110.186.5是192.168.1.5的外網ip):

[[email protected] ~]# 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

[[email protected] 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

[[email protected] conf.d]# curl http://192.168.1.23/proxy/

this is 192.168.1.5

[[email protected] 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

[[email protected] 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;

}

}

[[email protected] 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

[[email protected] 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/;

}

}

[[email protected] conf.d]# service nginx restart

Redirecting to /bin/systemctl restart  nginx.service

[[email protected] 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

[[email protected] 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;

}

}

[[email protected] conf.d]# service nginx restart

Redirecting to /bin/systemctl restart  nginx.service

[[email protected] 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

[[email protected] 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

[[email protected]st 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/;

}

}

[[email protected] 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

[[email protected] 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;

}

}

[[email protected] conf.d]# service nginx restart

Redirecting to /bin/systemctl restart  nginx.service

[[email protected] 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

[[email protected] 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/;

}

}

[[email protected] 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

[[email protected] 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;

}

}

[[email protected] 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/