在nginx.conf中更正代理路径

时间:2013-03-12 05:26:18

标签: nginx

我们有两台服务器,A和B.服务器A可在全球范围内访问。他安装了nginx。这就是我在conf中所拥有的:

location /test {
  proxy_pass http://localserver.com;
}

它应该做的是将addreess http://globalserver.com/test(即服务器A)转换为内部服务器地址http://localserver.com。但是,它确实附加了位置路径,即itries以查找根本不可用的http://localserver.com/test。如何将代理传递到正确的地址,丢弃该位置的最后一部分?

3 个答案:

答案 0 :(得分:39)

那应该有用。 Nginx应剥离上游本地服务器上的“/ test”路径。所以我能说的不是原因。为了使它更好一点,试试这个:

location /test/ {
  proxy_pass http://localserver.com/;
}

我在前两行添加的2个斜杠将避免错误地匹配'/ testABC',并将错误的请求发送到上游本地服务器,例如。

你有

吗?
proxy_redirect

在相同的位置块?如果您的上游本地服务器已重定向,那么该行上的错误将导致您所描述的问题。

[ UPDATE ]找到原始配置不起作用的原因并且我的工作原理:如果proxy_pass指令本身没有URI路径,则nginx不会替换URI路径部分。所以我在最后添加斜杠(斜杠被视为URI路径)的修复触发了URI路径替换。

参考:http://wiki.nginx.org/HttpProxyModule#proxy_pass

  

如果需要以未处理的形式传输URI,则应使用指令proxy_pass而不使用URI部分

location  /some/path/ {
  proxy_pass   http://127.0.0.1;
}

答案 1 :(得分:1)

尝试按此处http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass添加:

      proxy_pass http://localserver.com/;

答案 2 :(得分:-1)

尝试重写

location /test {
    rewrite ^ $scheme://$host/;
    proxy_pass http://localserver.com;
}

一些有用的链接......

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

http://wiki.nginx.org/Pitfalls

相关问题