【问题标题】:NGINX restrict IP addresses based on $argNGINX 根据 $arg 限制 IP 地址
【发布时间】:2021-10-05 18:25:16
【问题描述】:

使用 nginx 如何根据 $args 的正则表达式限制某些 IP 地址

例如

对于https://somewhere.invalid/login URI 位置

location /login {
    allow 1.2.3.4;
    deny all;
}

有道理

但我如何允许 /login 所有人,但受 IP 限制 where $args = "person=super"

eq https://somewhere.invalid/login?person=super

nginx 不允许在“if”块中使用“allow”语句。

location /login {
    allow all;
    if ( $args ~ /person=super/ ) {
        allow 1.2.3.4;
        deny all;
    }
}

如果位置块 / 是 proxy_pass 有什么区别吗?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    作为文档,says allowdeny 指令不能在 if 上下文中使用。允许的上下文是 httpserverlocationlimit_except。但是,您可以通过检查 $arg_person$remote_addr 变量值的两个 map 块来实现相同的行为:

    map $arg_person $deny {
        super    $checkip;
        # default value will be an empty string
    }
    map $remote_addr $checkip {
        1.2.3.4  '';
        # you can add other allowed IPs here
        default  1;
    }
    
    server {
        ...
        location /login {
            if ($deny) { return 403; }
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多