proxy_pass URL ,URL中是否含有URI,如果不包含,那么nginx服务器不会改变原地址的URI;如果包含了URI,则nginx服务器会使用新的URI替换原来的URI。来瞅瞅下面两个例子你就明白了。
如图在端口为18011的服务器上有 location模块的/admin/url 路由。
server { listen 18011; server_name location; location /admin/url{ proxy_pass http://127.0.0.1:18012; } }另一个服务器为18012有两个location模块路由。
server { listen 18012; server_name location; location /admin/url { echo "this is /admin/url ."; } location /url/test { echo "this is /url/test ."; } }我们基于上面两个虚拟服务来进行测试。
我们将18011的location模块的proxy_pass 代理到18012服务器.
server { listen 18011; server_name location; location /admin/url{ proxy_pass http://127.0.0.1:18012; } }通过浏览器访问 http://192.168.60.128:18011/admin/url 地址: 查看nginx访问日志信息如图: 可以看到原始请求URI没有改变。
我们在18011的location模块的proxy_pass URL中加入URI,并将其代理到18012服务器。
server { listen 18011; server_name location; location /admin/url{ proxy_pass http://127.0.0.1:18012/url/test; } }通过浏览器访问同一地址: http://192.168.60.128:18011/admin/url 查看nginx的访问日志信息: 我们可以发现在proxy_pass URL中加入URI后原始请求的URI被替换成了proxy_pass URL中的URI。
总结:在使用proxy_pass指令的时候,如果你想改变原地址中的URI,就只需在proxy_pass URL中加上URI。