全局禁用 Nginx 中所有站点的静态内容访问日志记录

全局禁用 Nginx 中所有站点的静态内容访问日志记录

我正在尝试全局禁用所有静态内容以及在同一个 nginx 实例上运行的所有 vhost 上的 access_log 和 log_not_found。

为了管理服务器 {} 块内的全局设置,我global/restrictions.conf在每个 vhost 的服务器 {} 块中包含了一个。

vhost 配置如下:

server {
        listen 1.2.3.4:80;
        server_name domain.com *.domain.com;
        access_log /var/domains/domain.com-access.log combined;
        error_log /var/domains/domain.com-error.log error;
        root /var/www/domain.com/;
        location / {
                index index.php index.html index.htm;
                try_files $uri $uri/ @rewrites;
        }
        location @rewrites {
                rewrite ^ /index.php last;
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/domain.com-php-fpm.socket;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }
        # Include global restrictions config
        include global/restrictions.conf;
}

global/restrictions.conf 中所有 vhost 的全局配置文件如下所示:

# Global restrictions configuration file.
# Will be included in the server {] block of each vhost.

# Disable logging for robots.txt files <--- WORKS
location = /robots.txt {
 allow all;
 log_not_found off;
 access_log off;
}

# Disable logging for all static files <--- DOES NOT WORK (WHY??)
location ~ \.(atom|bmp|bz2|css|doc|docx|eot|exe|gif|gz|ico|jpeg|jpg|js|mid|midi|mp4|ogg|ogv|otf|png|ppt|rar|rss|rtf|svg|svgz|swf|tar|tgz|ttf|txt|wav|woff|xml|xls|xlsx|zip)$ {
 log_not_found off;
 access_log off;
}

robots.txt 的规则似乎在所有 vhost 上都能正常工作,但不知何故,所有其他静态文件的正则表达式规则却不起作用,我似乎无法弄清楚原因。

有什么想法或建议吗?

答案1

location ~ \. 通过替换来修复它location ~* ^.+.

正则表达式导致了这个问题。

相关内容