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

 

在平常的web網站部署中,常常會用到nginx的proxy_pass反向代理,有一個配置須要弄清楚:配置proxy_pass時,當在後面的url加上了/,至關因而絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;若是沒有/,則會把匹配的路徑部分也給代理走(這樣配置在Nginx反向代理+負載均衡簡單實現(http方式)也提到過)。
下面舉個小實例說明下:
centos7系統庫中默認是沒有nginx的rpm包的,因此咱們本身須要先更新下rpm依賴庫php

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進行訪問測試-----------------html

爲了方便測試,先在另外一臺機器192.168.1.5上部署一個8090端口的nginx,配置以下: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)第一種狀況:web

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

[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的結果bash

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)第三種狀況curl

[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!
相關文章
相關標籤/搜索