代理的8种情况
模拟请求
curl localhost:8081/test/person/getinfo
配置情况及对应代理结果
#配置1
Location /test/ {
proxy_pass http://127.0.0.1:8000/api/;
}
# /api/ 替换 /test/person/getinfo 中的 /test/
对应结果 http://127.0.0.1:8000/api/person/getinfo
#配置2
Location /test {
proxy_pass http://127.0.0.1:8000/api;
}
# /api 替换 /test/person/getinfo 中的 /test
对应结果 http://127.0.0.1:8000/api/person/getinfo
#配置3
Location /test {
proxy_pass http://127.0.0.1:8000/api/;
}
# /api/ 替换 /test/person/getinfo 中的 /test
对应结果 http://127.0.0.1:8000/api//person/getinfo
#配置4
Location /test/ {
proxy_pass http://127.0.0.1:8000/api;
}
# /api 替换 /test/person/getinfo 中的 /test/
对应结果 http://127.0.0.1:8000/apiperson/getinfo
#配置5
Location /test/ {
proxy_pass http://127.0.0.1:8000/;
}
# / 替换 /test/person/getinfo 中的 /test/
对应结果 http://127.0.0.1:8000/person/getinfo
#配置6
Location /test/ {
proxy_pass http://127.0.0.1:8000;
}
# 不做替换
对应结果 http://127.0.0.1:8000/test/person/getinfo
#配置7
Location /test {
proxy_pass http://127.0.0.1:8000;
}
# 不做替换
对应结果 http://127.0.0.1:8000/test/person/getinfo
#配置8
Location /test {
proxy_pass http://127.0.0.1:8000/;
}
# / 替换 /test/person/getinfo 中的 /test
对应结果 http://127.0.0.1:8000//person/getinfo
分析结果
经过分析所有情况,发现其实就是用proxy_pass 端口后的内容去替换location地址 ,如果端口后没有内容则不做替换(斜杠也算内容)
eg:配置1,用http://127.0.0.1:8000/api/ 替换localhost:8081/test/person/getinfo ==> http://127.0.0.1:8000/api/person/getinfo
eg:配置4,用http://127.0.0.1:8000/api 替换localhost:8081/test/person/getinfo
==> http://127.0.0.1:8000/apiperson/getinfo
配置6、7比较特殊,端口后没有内容,所以只替换IP端口==>http://127.0.0.1:8000/test/person/getinfo