nginx 允许|拒绝 $realip_remote_addr

标签 nginx proxy cloudflare real-ip

使用 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 或本地主机的 IP 列入黑名单。这被证明是相当困难的,我试过在设置 real_ip 模块设置之前放置它,但没有雪茄。

这可能吗?如果用户不通过 cloudflare,这似乎是一个缺陷,它允许对某个 vhost 进行更多滥用。

有 $realip_remote_addr 变量,但我一辈子都找不到一种方法来允许/拒绝使用它来代替普通的 $remote_addr。

编辑:我注意到防火墙可以帮助解决这个问题。不幸的是,我真的只需要几个虚拟主机。

最佳答案

您可以使用地理 block 轻松地做到这一点

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
        }
    }

关于nginx 允许|拒绝 $realip_remote_addr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39176931/

相关文章:

javascript - Node.js 是否适合图像代理和操作任务?

node.js - Node JS Puppeteer 在带有代理的 url 上抛出错误

php - 通过 PHP/Curl 使用 CloudFlare API 返回错误

c# - 服务器错误 : The request was aborted: Could not create SSL/TLS secure channel

nginx - 有没有办法使用 "ngx_http_access_module"、 "ngx_http_limit_req_module"和 "ngx_http_realip_module",而不会踩到对方的脚趾?

django - 来自 AWS EC2 上重新定位的实例的 502 错误网关

http - http代理如何工作?

azure - 无法对 Azure 上托管的网站使用 Cloudflare SSL 证书

php - Nginx - 无法打开流 : Permission denied in/var/www/example. com

ruby - 使用 passenger + nginx 设置的 ruby​​ 应用程序上的 session 随机丢失