nginx代理proxy_pass絕對路徑和相對路徑實驗

在nginx中配置proxy_pass代理轉發時,如果在proxy_pass后面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走
假設下面四種情況分別用 http://127.0.0.1/proxy/cuffs/css/toosimple.txt 進行訪問,toosimple.txt文件里面寫文件路徑方便對比。

代理服務10.0.0.1:8080根目錄結構如下

static01
└── cuffs
└── css
└── toosimple.txt
static01cuffs
└── css
└── toosimple.txt
proxy
└── cuffs
└── css
└── toosimple.txt
cuffs
└── css
└── toosimple.txt

nginx代理配置

第一種絕對路徑

location /proxy/ {
         proxy_pass http://10.0.0.1:8080/;
    }

當訪問 http://127.0.0.1/proxy/cuffs/css/toosimple.txt時,nginx匹配到/proxy/路徑,把請求轉發給10.0.0.1:8080服務,實際請求代理服務器的路徑為
http://10.0.0.1:8080/cuffs/css/toosimple.txt

第二種相對路徑

location /proxy/ {
         proxy_pass http://10.0.0.1:8080;
    }

當訪問 http://127.0.0.1/proxy/cuffs/css/toosimple.txt時,nginx匹配到/proxy/路徑,把請求轉發給10.0.0.1:8080服務,實際請求代理服務器的路徑為
http://10.0.0.1:8080/proxy/cuffs/css/toosimple.txt 此時nginx會把匹配的proxy也代理給代理服務器

第三種

location /proxy/ {
         proxy_pass http://10.0.0.1:8080/static01/;
    }

當訪問 http://127.0.0.1/proxy/cuffs/css/toosimple.txt時,nginx匹配到/proxy/路徑,把請求轉發給10.0.0.1:8080服務,實際請求代理服務器的路徑為
http://10.0.0.1:8080/static01/cuffs/css/toosimple.txt

第四種

location /proxy/ {
         proxy_pass http://10.0.0.1:8080/static01;
    }

當訪問 http://127.0.0.1/proxy/cuffs/css/toosimple.txt時,nginx匹配到/proxy/路徑,把請求轉發給10.0.0.1:8080服務,實際請求代理服務器的路徑為
http://10.0.0.1:8080/static01cuffs/css/toosimple.txt

參考:http://m.blog.csdn.net/article/details?id=48803235

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容