当拒绝位置块中的所有内容时,NGINX 日志记录不起作用

当拒绝位置块中的所有内容时,NGINX 日志记录不起作用

I thought this behaviour was quite weird, hence I am going to ask on here.

Take this location block;

location ~ /(wp-config.php|readme.html|licence.txt|license.txt|readme.txt) {
    access_log /var/log/nginx/blocked.log blocked;
    deny all;
}

It does not log anything to the log file, however if I remove the deny all; - it does. Same goes for return 403 etc

I want to log blocked requests.

nginx version: nginx/1.8.0

UPDATE

The cause of this appears to be when you define your own error pages. For eg:

http {  ..
    error_page          403          /error/403.html;
    error_page          404          /error/404.html;
.. }

Removing these lines allowed logging

答案1

Things to check:

  1. Have you defined blocked?
  2. Or path to log file is correct?
  3. 检查location顺序,因为wp-config.php最有可能匹配location ~ \.php$或类似

nginx: 1.7.11.1尽管它是 Windows 环境,但它可以与 Gryphon 一起使用。

这是我的设置:

http {
    ...
    log_format robots   '$remote_addr - $remote_user [$time_local] '
                        '$host "$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
    ...
    server {

    ...
        location ~/(robots.txt|readme.html) {
            access_log  logs/nginx-access-robots.log robots;
            deny all;
        }
    ...
    }

}

相关内容