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

查看:87
本文介绍了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;
}

它应该做的是翻译地址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/;
}

例如,我在前 2 行添加的 2 个斜杠将避免错误匹配 '/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天全站免登陆