最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年11月03日 12:04:36   作者:散盡浮華  
這篇文章主要給大家介紹了關(guān)于nginx proxy_pass反向代理配置中url后加不加/的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

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

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

下面舉個小實例說明下:

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.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機(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)行訪問測試

為了方便測試,先在另一臺機(jī)器192.168.1.5上部署一個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/就會被代理到http://192.168.1.5:8090/。p匹配的proxy目錄不需要存在根目錄/var/www/html里面

注意,終端里如果訪問http://192.168.1.23/proxy(即后面不帶"/"),則會訪問失?。∫驗閜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的時候,會自動加上"/”(同理是由于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/,都會失??!

這樣配置后,訪問http://192.168.1.23/proxy/就會被反向代理到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就會被代理到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/,后面就算是默認(rèn)的index.html文件也要跟上,否則訪問失?。?/p>


-------------------------------------------------------------------------------------
上面四種方式都是匹配的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,和第三種結(jié)果一樣,同樣被代理到http://192.168.1.5:8090/haha/

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Ubuntu16.04.1 安裝Nginx的方法

    Ubuntu16.04.1 安裝Nginx的方法

    這篇文章主要介紹了Ubuntu16.04.1 安裝Nginx的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-01-01
  • Centos7.x下Nginx安裝及SSL配置與常用命令詳解

    Centos7.x下Nginx安裝及SSL配置與常用命令詳解

    這篇文章主要介紹了Centos7.x下Nginx安裝及SSL配置與常用命令詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • Nginx中proxy_pass的斜杠的兩種方式

    Nginx中proxy_pass的斜杠的兩種方式

    Nginx的官方文檔將proxy_pass分為不帶URI和帶URI兩種類型,本文就來介紹Nginx中proxy_pass的斜杠的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • CentOS下 安裝 Nginx的方法

    CentOS下 安裝 Nginx的方法

    這篇文章主要介紹了CentOS下 安裝 Nginx的方法,本文內(nèi)容簡單,通過實例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • nginx http重定向https配置說明

    nginx http重定向https配置說明

    這篇文章主要介紹了nginx http重定向https配置說明的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 詳解如何通過nginx進(jìn)行服務(wù)的負(fù)載均衡

    詳解如何通過nginx進(jìn)行服務(wù)的負(fù)載均衡

    負(fù)載均衡器可以將用戶請求根據(jù)對應(yīng)的負(fù)載均衡算法分發(fā)到應(yīng)用集群中的一臺服務(wù)器進(jìn)行處理,本文主要為大家詳細(xì)介紹了如何通過nginx進(jìn)行服務(wù)的負(fù)載均衡,需要的可以參考下
    2023-11-11
  • nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法

    nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法

    今天小編就為大家分享一篇nginx設(shè)置目錄白名單、ip白名單的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • 詳解Nginx反向代理實現(xiàn)會話(session)保持的兩種方式

    詳解Nginx反向代理實現(xiàn)會話(session)保持的兩種方式

    這篇文章主要介紹了詳解Nginx反向代理實現(xiàn)會話(session)保持的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Linux  安裝nginx服務(wù)器詳細(xì)介紹

    Linux 安裝nginx服務(wù)器詳細(xì)介紹

    這篇文章主要介紹了Linux 安裝nginx服務(wù)器詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Mac環(huán)境Nginx配置和訪問本地靜態(tài)資源的實現(xiàn)

    Mac環(huán)境Nginx配置和訪問本地靜態(tài)資源的實現(xiàn)

    這篇文章主要介紹了Mac環(huán)境Nginx配置和訪問本地靜態(tài)資源的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09

最新評論

张北县| 卢龙县| 普陀区| 桦川县| 泗洪县| 宝应县| 从化市| 永寿县| 甘谷县| 巍山| 吉林省| 中西区| 淮北市| 昂仁县| 武夷山市| 临泽县| 南宁市| 临沂市| 嘉禾县| 新沂市| 南丰县| 上饶市| 闵行区| 广安市| 正阳县| 财经| 泾源县| 芒康县| 沅江市| 沛县| 西和县| 衡山县| 讷河市| 西吉县| 武强县| 禹州市| 三河市| 荔浦县| 泸水县| 丹凤县| 大城县|