访问注销 nginx 不工作?

访问注销 nginx 不工作?

我有以下 drop.conf 文件,位于 /etc/nginx/drop.conf

# if you don't like seeing all the errors for missing favicon.ico requests
# sent by a lot of browsers in root we dont need to log these - they mean extra IO
location = /favicon.ico { access_log off; log_not_found off; }

# if you don't like seeing errors for a missing robots.txt in root
# same reason as above - extra IO
location = /robots.txt { allow all; access_log off; log_not_found off; }

location = /apple-touch-icon.png { access_log off; log_not_found off; }
location = /apple-touch-icon-precomposed.png { access_log off; log_not_found off; }

# this will prevent files like .htaccess .htpassword .secret .git .svn etc from being served
# You can remove the log directives if you wish to
# log any attempts at a client trying to access a hidden file
location ~ /\. { deny all; access_log off; log_not_found off; }

# Deny access to any files with a .php extension in the uploads directory
location ~* ^/wp-content/uploads/.*.php$ {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny access to any files with a .php extension in the uploads directory for multisite
location ~* /files/(.*).php$ {
    deny all;
    access_log off;
    log_not_found off;
}

奇怪的是,当我将其包含在任何服务器指令中(即,无论我配置了哪个虚拟主机),我的访问日志中仍然会收到图标日志。这是一个 BUG 吗?看起来包含根本不起作用?

我将其包含在内的虚拟主机示例:

server {
        listen       127.0.0.1:8080;
        server_name  .somehost.com;
    root  /var/www/somehost.com;

        access_log /var/log/nginx/somehost.com-access.nginx.log main;
    error_log  /var/log/nginx/somehost.com-error.nginx.log;

        location ~* \.php.$ {
        # Proxy all requests with an URI ending with .php*
        # (includes PHP, PHP3, PHP4, PHP5...)
        include /etc/nginx/fastcgi.conf;
        }

        # all other files
        location / {
            root  /var/www/somehost.com;
        }

        error_page 404 /errors/404.html;
        location /errors/ {
                alias /var/www/errors/;
                internal;
        }

        #this loads custom logging configuration which disables favicon error logging
        include /etc/nginx/drop.conf;
}

但在该域的访问日志中我仍然看到:

***** - - [06/Jul/2012:22:16:05 +0000]  "GET /favicon.ico HTTP/1.1" 404 134 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11"

是的,我已经重新启动并重新加载了 nginx。

这实际上非常有趣。当我直接向上面的虚拟主机添加特定位置指令并禁用包含 favicon.ico 请求时,请求仍然被记录。即我在包含位中注释并将以下内容添加到上面的虚拟主机:

#include /etc/nginx/drop.conf;
location = /favicon.ico { access_log off; log_not_found off; }

很奇怪。

答案1

我会将 drop.conf 放在 conf 的较早位置(就在 root 指令之后)。

还要确保重新加载或重新启动 nginx,以便它使用新的配置。

答案2

我正在研究同样的问题,据我所知,favicon 404 会生成一个子请求,子请求不会继承调用它们的位置的设置。我排除 404(和其他)响应日志记录的解决方案如下:

location = /favicon.ico 
{ 
    log_not_found off; 
    log_subrequest off; 
    access_log off; 
    error_page 404 /404notlogged.html; 
    try_files $uri =404; 
} 

location = /404notlogged.html 
{ 
    expires +1y; 
    log_not_found off; 
    log_subrequest off; 
    access_log off; 
    internal; 
}

相关内容