nginx配置proxy_pass转发路径问题解决方法
时间:2014-10-31 22:25 来源:linux.it.net.cn 作者:it
在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
如果没有/,则会把匹配的路径部分也给代理走。
复制代码代码示例:
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com/;
}
按照以上配置,如果请求的url是http://servername/static_js/test.html
会被代理成http://js.test.com/test.html
如果按如下配置:
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
proxy_pass http://js.test.com;
}
则会被代理到http://js.test.com/static_js/test.htm
也可以用rewrite实现/的功能,例如:
location ^~ /static_js/
{
proxy_cache js_cache;
proxy_set_header Host js.test.com;
rewrite /static_js/(.+)$ /$1 break;
proxy_pass http://js.test.com;
}
(责任编辑:IT)
在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;
复制代码代码示例:
location ^~ /static_js/
{ proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com/; }
按照以上配置,如果请求的url是http://servername/static_js/test.html
如果按如下配置:
location ^~ /static_js/
{ proxy_cache js_cache; proxy_set_header Host js.test.com; proxy_pass http://js.test.com; } 则会被代理到http://js.test.com/static_js/test.htm
也可以用rewrite实现/的功能,例如:
location ^~ /static_js/
{ proxy_cache js_cache; proxy_set_header Host js.test.com; rewrite /static_js/(.+)$ /$1 break; proxy_pass http://js.test.com; } |