欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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

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

下面舉個小實例說明下:

centos7系統(tǒng)庫中默認是沒有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機器的外網(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進行訪問測試

為了方便測試,先在另一臺機器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反向代理機器,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(即后面不帶"/"),則會訪問失??!因為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的結(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/,后面就算是默認的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,和第三種結(jié)果一樣,同樣被代理到http://192.168.1.5:8090/haha/

總結(jié)

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

相關(guān)文章

  • Nginx實現(xiàn)異步訪問mysql的配置方法

    Nginx實現(xiàn)異步訪問mysql的配置方法

    這篇文章主要介紹了Nginx實現(xiàn)異步訪問mysql的配置方法,本文先是講解了安裝配置方法,然后給出了使用方法,需要的朋友可以參考下
    2015-06-06
  • nginx 代理后出現(xiàn)503的解決方法

    nginx 代理后出現(xiàn)503的解決方法

    本文主要介紹了nginx 代理后出現(xiàn)503的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • nginx泛域名解析配置教程

    nginx泛域名解析配置教程

    這篇文章主要介紹了nginx泛域名解析配置教程,需要的朋友可以參考下
    2017-01-01
  • upstream模塊在nginx配置文件中的作用詳解

    upstream模塊在nginx配置文件中的作用詳解

    這篇文章主要為大家介紹了upstream模塊在nginx配置文件中的作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Nginx進程調(diào)度問題詳解

    Nginx進程調(diào)度問題詳解

    Nginx采用的是固定數(shù)量的多進程模型,由一個主進程(MasterProcess)和數(shù)量與主機CPU核數(shù)相同的工作進程協(xié)同處理各種事件。這篇文章主要介紹了Nginx進程調(diào)度問題,需要的朋友可以參考下
    2021-09-09
  • Nginx配置及熱升級的詳細介紹

    Nginx配置及熱升級的詳細介紹

    Nginx與Apache一樣,都是web服務(wù)器,但是Nginx比Apache多一些功能,比如Nginx可以做代理,可以做負載均衡,這篇文章主要介紹了Nginx配置以及熱升級,需要的朋友可以參考下
    2024-07-07
  • nginx could not build the server_names_hash 解決方法

    nginx could not build the server_names_hash 解決方法

    服務(wù)器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。
    2011-03-03
  • Nginx中透傳客戶端真實IP的技巧

    Nginx中透傳客戶端真實IP的技巧

    為了記錄日志、限制訪問或進行其他基于?IP?地址的操作,獲取客戶端的真實?IP?地址非常重要,本文就來詳細的介紹一下Nginx中透傳客戶端真實IP的技巧,感興趣的可以了解一下
    2024-08-08
  • 在Nginx服務(wù)器中啟用SSL的配置方法

    在Nginx服務(wù)器中啟用SSL的配置方法

    這篇文章主要介紹了在Ningx服務(wù)器中啟用SSL的配置方法,本文前提是已經(jīng)在Linux系統(tǒng)下安裝好了OpenSSL,需要的朋友可以參考下
    2015-08-08
  • 18個運維必知的Nginx代理緩存配置技巧(你都掌握了哪些呢)

    18個運維必知的Nginx代理緩存配置技巧(你都掌握了哪些呢)

    這篇文章主要介紹了18個運維必知的Nginx代理緩存配置技巧(你都掌握了哪些呢),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論