nginx location proxy_pass詳解


在nginx中配置proxy_pass時,如果在proxy_pass后面的url加/,相當於是絕對根路徑,則nginx不會把location中匹配的路徑部分代理走;如果沒有/,則會把匹配的路徑部分給代理走。 

下面四種情況分別用http://106.12.74.123/abc/index.html進行訪問。

# 第一種
location  /abc
    {
        proxy_pass http://106.12.74.123:83/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 結論:會被代理到http://106.12.74.123/index.html 這個url
 
 
 
# 第二種(相對於第一種,最后少一個 /)
location  /abc
    {
        proxy_pass http://106.12.74.123:83;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 結論:會被代理到http://106.12.74.123/abc/index.html 這個url
 
 
 
第三種:
location  /abc
    {
        proxy_pass http://106.12.74.123:83/linux/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 結論:會被代理到http://106.12.74.123/linux/index.html 這個url。
 
 
 
# 第四種(相對於第三種,最后少一個 / ):
location  /abc
    {
        proxy_pass http://106.12.74.123:83/linux;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
# 結論:會被代理到http://106.12.74.123/linuxindex.html 這個url

下面我們驗證一下:

環境准備:nginx的配置如下

主配置文件:

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
  
    location  /abc
    {
        proxy_pass http://106.12.74.123:83/;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

副配置文件,上面是代理到83端口了,我們看一下83端口的配置為文件,之前做其他實驗,現在我們就在這個上面簡單修改下。

[root@master conf.d]# cat 83.conf 
server
{
   listen 83;
   server_name 106.127.74.123;
   root /usr/share/nginx/83;
}

查看靜態文件地址:

[root@master nginx]# tree 83/
83/
├── abc
│   └── index.html
├── index.html
└── linux
    └── index.html
 
[root@master nginx]# cat 83/index.html 
83
 
[root@master nginx]# cat 83/abc/index.html 
83  abc
 
[root@master nginx]# cat 83/linux/index.html 
linux

第一種:當我們http://106.12.74.123/abc/index.html時候,頁面出現的是83

第二種:當我們http://106.12.74.123/abc/index.html時候,頁面出現的是83  abc的內容

第三種:當我們http://106.12.74.123/abc/index.html時候,頁面出現的是linux的內容

第四種:當我們http://106.12.74.123/abc/index.html時候,頁面出現的是404  因為83目錄下並沒有linxuindex.html的文件
————————————————
原文鏈接:https://blog.csdn.net/wyl9527/article/details/89671506


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM