麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > Nginx > 正文

nginx proxy_pass反向代理配置中url后加不加/的區別介紹

2024-08-30 12:29:16
字體:
來源:轉載
供稿:網友

前言

nginx作為web服務器一個重要的功能就是反向代理。nginx反向代理的指令不需要新增額外的模塊,默認自帶proxy_pass指令,只需要修改配置文件就可以實現反向代理。

而在日常的web網站部署中,經常會用到nginx的proxy_pass反向代理,有一個配置需要弄清楚:配置proxy_pass時,當在后面的url加上了/,相當于是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分也給代理走(這樣配置可以參考這篇文章)。

下面舉個小實例說明下:

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.confserver {listen 80;server_name localhost;location / {root /var/www/html;index index.html;}} [root@localhost conf.d]# cat /var/www/html/index.htmlthis 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.23this 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.confserver {listen 8090;server_name localhost;location / {root /var/www/html;index index.html;}}[root@bastion-IDC ~]# cat /var/www/html/index.htmlthis 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:8090this is 192.168.1.5

nginx,proxy,pass配置,pass,proxypass,配置

192.168.1.23作為nginx反向代理機器,nginx配置如下:

1)第一種情況:

[root@localhost conf.d]# cat test.confserver {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的結果

nginx,proxy,pass配置,pass,proxypass,配置

2)第二種情況,proxy_pass配置的url后面不加"/"

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting 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/

nginx,proxy,pass配置,pass,proxypass,配置

3)第三種情況

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting 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/

nginx,proxy,pass配置,pass,proxypass,配置

4)第四種情況:相對于第三種配置的url不加"/"

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting to /bin/systemctl restart nginx.service[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html192.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.html192.168.1.5 hahaindex.html

注意,這種情況下,不能直接訪問http://192.168.1.23/proxy/,后面就算是默認的index.html文件也要跟上,否則訪問失??!

nginx,proxy,pass配置,pass,proxypass,配置

-------------------------------------------------------------------------------------
上面四種方式都是匹配的path路徑后面加"/",下面說下path路徑后面不帶"/"的情況:

1)第一種情況,proxy_pass后面url帶"/":

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting to /bin/systemctl restart nginx.service

nginx,proxy,pass配置,pass,proxypass,配置

nginx,proxy,pass配置,pass,proxypass,配置

2)第二種情況,proxy_pass后面url不帶"/"

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting 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/

nginx,proxy,pass配置,pass,proxypass,配置

3)第三種情況

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting 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/

nginx,proxy,pass配置,pass,proxypass,配置

4)第四種情況:相對于第三種配置的url不加"/"

[root@localhost conf.d]# cat test.confserver {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 restartRedirecting to /bin/systemctl restart nginx.service

nginx,proxy,pass配置,pass,proxypass,配置

這樣配置的話,訪問http://103.110.186.23/proxy,和第三種結果一樣,同樣被代理到http://192.168.1.5:8090/haha/

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 最新中文字幕免费视频 | 久久精品中文字幕一区二区 | 黄色成人av在线 | 毛片免费一区二区三区 | 黄色片视频免费观看 | 国产成年免费视频 | 日本精品二区 | 久久精品中文字幕一区 | 欧美精品色精品一区二区三区 | 日韩视频一区二区 | 3级毛片| 男人久久天堂 | 黄色午夜剧场 | 27xxoo无遮挡动态视频 | 国产麻豆交换夫妇 | 污在线观看网站 | 成人宗合网 | 国产精品久久久久久久久久久久久久久 | 日本黄色免费片 | 久久久国产精品免费观看 | 国产影视 | 羞羞视频免费视频欧美 | 欧美18一19sex性护士农村 | 国产一级二级在线播放 | 天堂成人国产精品一区 | 国产亚洲精品久久午夜玫瑰园 | 性 毛片 | 精品一区二区亚洲 | 99爱在线免费观看 | 中午字幕无线码一区2020 | 日本韩国欧美一级片 | 精品国产观看 | 斗破苍穹在线观看免费完整观看 | 国产一及毛片 | 国产一区二区三区高清 | 日本黄色一级视频 | 国产精品久久久久无码av | 欧美成a人片在线观看久 | 日本看片一区二区三区高清 | 特一级毛片 | 国产永久免费观看 |