【问题标题】:Nginx block from referrer来自推荐人的 Nginx 块
【发布时间】:2014-07-29 03:19:43
【问题描述】:

我需要阻止所有具有引荐来源 click2dad.net 的 http 连接。 我在 mysite.conf 中写:

 location / {
            valid_referers ~.*http://click2dad\.net.*;
            if ($invalid_referer = ''){
                    return 403;
            }
            try_files       $uri    $uri/   /index.php?$args;
    }

但我仍然在 nginx 日志中看到:

HTTP/1.1" 200 26984 "http://click2dad.net/view/VUhfCE4ugTsb0SoKerhgMvPXcmXszU" 

200,而不是 403

因为阻止所有客户端访问 click2dad.net 是正确的吗?

【问题讨论】:

  • 谢谢大家,我只是拒绝在 nginx 配置中打开 iframe,并且 click2dad 可能无法获取我的网站。

标签: nginx spam-prevention referrer-spam


【解决方案1】:

valid_referers 解决方案有效,但对我个人而言,很难以这种方式维持长长的黑名单。另一种解决方案是使用ngx_http_map_module 模块。在 ubuntu 14.04 nginx 发行版下,您将创建一个 /etc/nginx/blacklist.conf 文件:

# /etc/nginx/blacklist.conf

map $http_referer $bad_referer {
    hostnames;

    default                           0;

    # Put regexes for undesired referers here
    "~social-buttons.com"             1;
    "~semalt.com"                     1;
    "~kambasoft.com"                  1;
    "~savetubevideo.com"              1;
    "~descargar-musica-gratis.net"    1;
    "~7makemoneyonline.com"           1;
    "~baixar-musicas-gratis.com"      1;
    "~iloveitaly.com"                 1;
    "~ilovevitaly.ru"                 1;
    "~fbdownloader.com"               1;
    "~econom.co"                      1;
    "~buttons-for-website.com"        1;
    "~buttons-for-your-website.com"   1;
    "~srecorder.co"                   1;
    "~darodar.com"                    1;
    "~priceg.com"                     1;
    "~blackhatworth.com"              1;
    "~adviceforum.info"               1;
    "~hulfingtonpost.com"             1;
    "~best-seo-solution.com"          1;
    "~googlsucks.com"                 1;
    "~theguardlan.com"                1;
    "~i-x.wiki"                       1;
    "~buy-cheap-online.info"          1;
    "~Get-Free-Traffic-Now.com"       1;
}

然后将其包含在您的 /etc/nginx/nginx.conf 文件中:

# /etc/nginx/nginx.conf


    http {
    # ...

      include blacklist.conf;

    # ...

    }

完成后,您可以在您的 nginx 站点中检查 $bad_referer 条件:

# /etc/nginx/sites-enabled/mysite.conf

server {
  # ...

  if ($bad_referer) { 
    return 444; 
  } 

  # ...
}

为了确保这些东西正常工作,你可以在你的 shell 中执行一个类似的命令:

$ curl --referer http://www.social-buttons.com https://example.org

...这应该给你:

curl: (52) Empty reply from server

我在这里https://fadeit.dk/blog/2015/04/22/nginx-referer-spam-blacklist/ 写了一篇关于这个解决方案的博文。

【讨论】:

  • 这些正则表达式将匹配other-social-buttons.comgoogle.com/page-about-social-buttons.comsocial-buttons.com.uk不仅 social-buttons.com。为了安全起见,您可以使用"~*^https?://(www.)?social-buttons\.com(/.*)?$" 1;(它将测试以httpshttp 和可选www. 开头的引用。它以~* 开头,这意味着它是不区分大小写的正则表达式。您应该转义点像这样\. 它以(/.*)?$ 结尾,不包括其他域。当我测试hostnames; 没有任何效果(nginx/1.14.0),但你可以使用它们$http_host 地图。
  • 博文底部的链接已损坏。
【解决方案2】:

需要注意的是,表达式将与“http://”或“https://”之后的文本匹配 http://nginx.org/en/docs/http/ngx_http_referer_module.html

正确的配置:

 location / {
            valid_referers click2dad.net*;
            if ($invalid_referer = ''){
                    return 403;
            }
            try_files       $uri    $uri/   /index.php?$args;
    }

【讨论】:

  • 你能显示完整的 nginx 配置吗?可能是在工作地点或其他方面。
  • 添加星号。我在我的服务器上测试了这个配置(超过域),它返回 403。
【解决方案3】:

也许你可以试试这个配置:

 location / {
        valid_referers ~click2dad.net;
        if ($invalid_referer){
                return 403;
        }
        try_files       $uri    $uri/   /index.php?$args;
}

无论如何,正确的答案就在这份文件中。 http://nginx.org/en/docs/http/ngx_http_referer_module.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2011-01-03
    相关资源
    最近更新 更多