仅允许 nginx 中允许的引用者并记录阻止的日志

仅允许 nginx 中允许的引用者并记录阻止的日志

别用力打,在google里没找到。

我需要仅允许允许的引荐来源域和空引荐来源访问我的网站。阻止其他引荐来源并记录。它最多可以有 20 个引荐来源,所以我需要一种数组。nginx 1.10.0 标准版,ubuntu 16.04

我尝试制作地图,但不起作用。1. 不要阻止。2. 不允许我登录 if {} 上下文

  log_format myref '"$http_referer" "$http_user_agent"';

  map $http_referer $allow_referer {
            default                  0;
            "~medirl.net"       1;
            "~facebook.com"       1;
            "~clickear.com"       1;
        }

server {
        listen *:80;

        if ($allow_referer = false) {
                return 444;
               access_log /var/log/domain.com/accessBADREF.log myref;
        }

答案1

首先:你应该使用Nginx Referer 模块为此。
对于你的情况,你可以:

  1. if ()使用if=$disallow_referer参数将 access_log 移到外部
  2. 使用地图名称disallow_referer
  3. 映射default1并允许引用""
  4. 使用if ($disallow_referer) {条件

详细信息:

map $http_referer $allow_referer {
        default                  0;
        "~medirl.net"       1;
        "~facebook.com"      1;
        "~clickear.com"       1;
    }
map $allow_referer $disallow_referer {
    0       1;
    1       "";
}

server {
   listen 127.0.0.1:9999 default_server;

   root /st/hosting/demo/htdocs;
   access_log /var/log/nginx/access_default.log combined if=$allow_referer;
   access_log /var/log/nginx/accessBADREF.log myref if=$disallow_referer;
   if ($disallow_referer) { return 444; }

    # Usage of Nginx Referer Module:
    # valid_referers *medirl.net *facebook.com *clickear.com;
    # if ($invalid_referer) { return 403; }
}

user@server> sudo nginx -t && sudo pkill -HUP nginx
user@server> curl -s -v -e "http://aaa.com" http://127.0.0.1:9999/1.txt 2>&1 | tail -n2
* Empty reply from server
* Connection #0 to host 127.0.0.1 left intact

user@server> curl -s -e "http://facebook.com" http://127.0.0.1:9999/1.txt                    
He has been known by many names;  the Prince of Lies, the Director, Lucifer,
Belial, and once, at a party, some obnoxious drunk kept calling him "Dude".
            -- Stig's Inferno

相关内容