nginx条件代理通行证 [英] nginx conditional proxy pass

查看:90
本文介绍了nginx条件代理通行证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置nginx代理以将请求传递给另一台服务器, 仅当$ request_body变量与特定的正则表达式匹配时.

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.

我目前只限于此:

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?

预先感谢

推荐答案

Nginx路由基于与请求URI匹配的location指令.解决方案是临时修改此设置,以将请求转发到不同的端点.

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.

server {
    listen 80 default;
    server_name test.local;

     if ($request_body ~* ^(.*)\.test) {
         rewrite ^(.*)$ /istest/$1;
     }

     location / {
         root /srv/http;
     }

     location /istest/ {
        rewrite ^/istest/(.*)$  $1 break;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://www.google.de;

    }
}

只有在重写模块是它的一部分.在这个例子中. rewrite在请求URI的前面加上istest.

The if condition can only safely be used in Nginx with the rewrite module which it is part of. In this example. The rewrite prefixes the Request URI with istest.

location块优先于最接近的匹配.匹配/istest/的所有内容都将转到第二个块,该块使用另一个rewrite从请求URI中删除/istest/,然后再转发到上游代理.

The location blocks give precedence to the closest match. Anything matching /istest/ will go to the second block which uses another rewrite to remove /istest/ from the Request URI before forwarding to the upstream proxy.

这篇关于nginx条件代理通行证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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