I'm trying to globally disable access_log and log_not_found on all static content and for all vhosts running on the same nginx instance.

In order to manage global settings inside the server {} block, I do include a global/restrictions.conf in the server {} block of each vhost.

The vhost configs look like:

server {
        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;

The global config file for all vhosts at global/restrictions.conf looks like:

# 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;

The rule for the robots.txt seems to work fine on all vhosts, but somehow the rule with the regex for all the other static files won't work and I cant seem to figure out why.

Any ideas or suggestions?

  • And we've all acquired a little more insight into that famous "now you've got two problems" adage regarding regular expressions. :) – HopelessN00b Feb 13 '15 at 21:10

Fixed it by replacing location ~ \. with location ~* ^.+.

The regex caused the issue.

  • perhaps add a slash to ensure there is a literal dot before the extension: location ~* ^.+\. – Paul S Oct 19 '15 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy