在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