日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/1/21 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【nginx配置】 proxy_pass反向代理配置中url后面加不加/的说明 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在日常的web網(wǎng)站部署中,經(jīng)常會(huì)用到nginx的proxy_pass反向代理,有一個(gè)配置需要弄清楚:配置proxy_pass時(shí),當(dāng)在后面的url加上了/,相當(dāng)于是絕對根路徑,則nginx不會(huì)把location中匹配的路徑部分代理走;如果沒有/,則會(huì)把匹配的路徑部分也給代理走(這樣配置在Nginx反向代理+負(fù)載均衡簡單實(shí)現(xiàn)(http方式)也提到過)。
下面舉個(gè)小實(shí)例說明下:
centos7系統(tǒng)庫中默認(rèn)是沒有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.rpm2)使用下面命令安裝nginx [root@localhost ~]# yum install nginx3)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)啟動(dòng)Nginx [root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service5)測試訪問(103.110.186.23是192.168.1.23機(jī)器的外網(wǎng)ip) [root@localhost conf.d]# curl http://192.168.1.23 this is page of test!!!!


--------看看下面幾種情況:分別用http://192.168.1.23/proxy/index.html進(jìn)行訪問測試--------

為了方便測試,先在另一臺(tái)機(jī)器192.168.1.5上部署一個(gè)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的外網(wǎng)ip): [root@bastion-IDC ~]# curl http://192.168.1.5:8090 this is 192.168.1.5


192.168.1.23作為nginx反向代理機(jī)器,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/就會(huì)被代理到http://192.168.1.5:8090/。p匹配的proxy目錄不需要存在根目錄/var/www/html里面
注意,終端里如果訪問http://192.168.1.23/proxy(即后面不帶"/"),則會(huì)訪問失敗!因?yàn)閜roxy_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的時(shí)候,會(huì)自動(dòng)加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的結(jié)果

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/,都會(huì)失敗! 這樣配置后,訪問http://192.168.1.23/proxy/就會(huì)被反向代理到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就會(huì)被代理到http://192.168.1.5:8090/hahaindex.html 同理,訪問http://192.168.1.23/proxy/test.html就會(huì)被代理到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/,后面就算是默認(rèn)的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會(huì)自動(dòng)加上"/”(即變成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會(huì)自動(dòng)加上"/”(即變成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,和第三種結(jié)果一樣,同樣被代理到http://192.168.1.5:8090/haha/

如下一簡單配置示例

只有當(dāng)訪問http://www.kevin.com/los/.....的時(shí)候才代理負(fù)載到http://192.168.10.24:50006/los/.... 和 http://192.168.10.25:50006/los/....上, 也就是說訪問www.kevin.com域名, 只有在后面匹配los路徑時(shí)才代理負(fù)載到192.168.10.24/25的50006端口的los路徑下, 除此之外, 訪問域名 www.kevin.com 匹配其他任何路徑(包括/, 即http://www.kevin.com) 時(shí)都跳轉(zhuǎn)到一個(gè)錯(cuò)誤頁面:[root@external-lb02 vhosts]# cat 80-www.kevin.com.confupstream 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;} }錯(cuò)誤頁面設(shè)置: [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

總結(jié)

以上是生活随笔為你收集整理的【nginx配置】 proxy_pass反向代理配置中url后面加不加/的说明的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。