【问题标题】:nginx allow|deny $realip_remote_addrnginx 允许|拒绝 $realip_remote_addr
【发布时间】:2016-08-27 02:39:28
【问题描述】:

使用 nginx,您可以允许和拒绝范围和 ips (https://www.nginx.com/resources/admin-guide/restricting-access/)。 使用realip模块,可以将它使用的ip改成cloudflare后的真实IP。 (http://nginx.org/en/docs/http/ngx_http_realip_module.html)

现在是这样,我想将任何不是 Cloudflare 或 localhost 的 ip 列入黑名单。事实证明这相当困难,我尝试在设置 real_ip 模块设置之前放置它,并且没有雪茄。

这可能吗?如果用户没有通过 cloudflare,这似乎是一个缺陷,它允许对某个虚拟主机进行更多的滥用。

有 $realip_remote_addr 变量,但我一辈子都无法找到一种方法来让允许/拒绝使用它而不是正常的 $remote_addr。

编辑:我注意到防火墙可以在这方面提供帮助。不幸的是,我真的只需要几个虚拟主机。

【问题讨论】:

    标签: nginx proxy cloudflare real-ip


    【解决方案1】:

    您可以使用地理块轻松完成此操作

    geo $realip_remote_addr $giveaccess {
          default 0;
          IPBLOCK1 1;
          IPBLOCK2 1;
          …
        }
        server {
           …
           location / {
             if ($giveaccess = 0){
              return 403 "$realip_remote_addr";
              #use it for debug
            }
        }
    

    【讨论】:

      【解决方案2】:

      我认为您想阻止直接访问您的机器,即通过您机器的 ip。

      您可以为此使用 cloudflare 设置的 http 标头。每当通过 cloudflare 发出请求时,它会将$http_cf_connecting_ip 设置为访问您机器的机器的 ip。

      所以你可以写一个条件,拒绝所有$http_cf_connecting_ip为空的请求。

      【讨论】:

      • 从来没想过!谢谢。请注意,您还可以检查 $remote_addr == $realip_remote_addr
      • 如果解决了您的问题,请点赞并接受。 :)
      • 这不安全!!!任何人都可以添加自己的 CF-Connecting-IP 标头
      【解决方案3】:

      这样的简单可以成就伟大的事情。

      使用$realip_remote_addr == $remote_addr 更聪明,因为$http_* 可以被发送此类请求的客户端伪造。 (一位朋友向我指出)。

      编辑:好的,考虑到以上几点,最好使用 Lua 模块。

      类似于下面的内容。

      access_by_lua_block {
          if ngx.var.remote_addr == ngx.var.realip_remote_addr then
                  return ngx.redirect("http://somewhere.else.please/");
          end
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-14
        • 2015-01-14
        • 2014-09-03
        • 2014-10-23
        • 1970-01-01
        • 2016-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多