nginx 条件代理传递
我正在尝试配置 nginx 来代理将请求传递到另一台服务器, 仅当 $request_body 变量与特定正则表达式匹配时。
我现在的问题是,我不知道如何准确配置此行为。
我目前正在考虑这一点:
server {
listen 80 default;
server_name test.local;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
if ($request_body ~* ^(.*)\.test) {
proxy_pass http://www.google.de;
}
root /srv/http;
}
}
但这里的问题是,根总是占上风。 无论哪种方式,代理都不会被传递。
我知道如何才能做到这一点吗?
提前致谢
i am trying to configure nginx to proxy pass the request to another server,
only if the $request_body variable matches on a specific regular expression.
My problem now is, that I don't how to configure this behaviour exactly.
I am currently down to this one:
server {
listen 80 default;
server_name test.local;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
if ($request_body ~* ^(.*)\.test) {
proxy_pass http://www.google.de;
}
root /srv/http;
}
}
but the problem here is, that root has always the upperhand.
the proxy won't be passed either way.
any idea on how I could accomplish this?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this:
Nginx 路由基于与请求 URI 相匹配的位置指令。解决方案是临时修改此设置,以便将请求转发到不同的端点。
if
条件只能在带有 重写模块的 Nginx 中安全使用 它是其中的一部分。在这个例子中。rewrite
在请求 URI 中添加istest
前缀。location
块优先考虑最接近的匹配项。任何匹配/istest/
的内容都将转到第二个块,该块在转发到上游之前使用另一个rewrite
从请求 URI 中删除/istest/
代理人。Nginx routing is based on the location directive which matches on the Request URI. The solution is to temporarily modify this in order to forward the request to different endpoints.
The
if
condition can only safely be used in Nginx with the rewrite module which it is part of. In this example. Therewrite
prefixes the Request URI withistest
.The
location
blocks give precedence to the closest match. Anything matching/istest/
will go to the second block which uses anotherrewrite
to remove/istest/
from the Request URI before forwarding to the upstream proxy.