正确的nginx.conf中的代理路径 [英] Correct proxy path in nginx.conf

查看:105
本文介绍了正确的nginx.conf中的代理路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个服务器A和B.服务器A在全球范围内都可以访问.他已经安装了nginx.那就是我在conf中拥有的东西:

we have two servers, A and B. Server A is accessed worldwide. He has nginx installed. That's what I have in conf:

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

它应该做的是翻译addreess http://globalserver.com/test (即服务器A)到内部服务器地址 http://localserver.com .但是,它确实附加了位置路径,即试图查找 http://localserver.com/test ,这根本不可用.如何使代理传递到正确的地址,而将位置的最后一部分扔掉?

What it should do, is translate the addreess http://globalserver.com/test (that is server A) to internal server address http://localserver.com. However, it does append the location path, that is, itries to look for http://localserver.com/test, which is not available at all. How can I make the proxy pass to the correct address, throwing out the last part in the location?

推荐答案

应该可以. Nginx应该剥离上游本地服务器上的"/test"路径.所以我只能说这不是原因.为了使它更好一点,请尝试以下操作:

That should work. Nginx should strip the '/test' path on the upstream local server. So what I can say is that is not the cause. To make it a bit better, try this:

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

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

The 2 slashes I added at the first 2 lines will avoid mistakenly match '/testABC' and send the wrong request to the upstream local server, for example.

你有一个

proxy_redirect

行在同一位置块吗?如果您的上游本地服务器具有重定向,则该行上的错误将导致您所描述的问题.

line in the same location block? If your upstream local server has redirects, then a mistake on that line will cause an issue like you described.

[ UPDATE ]找到了原始配置无法正常工作并且我的正常运行的根本原因:如果proxy_pass指令本身没有URI路径,nginx不会替换URI路径部分.因此,我在末尾添加斜杠(斜杠被视为URI路径)的方法会触发URI路径替换.

[UPDATE] Found the root cause why the original config didn't work and mine works: nginx does NOT replace URI path part if the proxy_pass directive does not have a URI path itself. So my fix of adding a slash (slash is treated as a URI path) at the end triggers the URI path replacement.

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

如果必须以未处理的形式传输URI,则应使用不带URI部分的指令proxy_pass

If it is necessary to transmit URI in the unprocessed form then directive proxy_pass should be used without URI part

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

这篇关于正确的nginx.conf中的代理路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆