location ~* \.(...) access_log off; prevents access to files instead of logs

Maxim Dounin mdounin at mdounin.ru
Tue Nov 14 15:52:14 UTC 2023


Hello!

On Tue, Nov 14, 2023 at 02:51:05PM +0100, Roberto D. Maggi wrote:

> Hi you all,
> I'm having a problem with these two stanzas, in writing down a virtual
> host and can't figure out what's wrong with them.
> They look correct but the first doesn't simply work and the second blocks
> 
> --> here I'm trying to add this header only to cgi|shtml|phtml|php 
> extensions
> 
> location ~* \.(?:cgi|shtml|phtml|php)$ {
>        add_header Cache-Control "public";
>        client_max_body_size 0;
>        chunked_transfer_encoding on;
>        }
> 
> --> here I don't want to log accesses to to 
> woff|woff2|ico|pdf|flv|jpg|jpeg|png|gif|js|css|gz|swf|txt files
> location ~* 
> \.(?:woff|woff2|ico|pdf|flv|jpg|jpeg|png|gif|js|css|gz|swf|txt)$ {
>        access_log off;
>        }
> 
> 
> Does anybody can guess what's wrong with them?
> Thanks in advance.

When processing a particular request, nginx selects a 
location and handles a request according to the configuration in 
this location, see http://nginx.org/r/location.

As such, the first location, which tries to alter processing of 
php files, does not seem to be correct: in particular, it lacks 
any fastcgi_pass / proxy_pass directives, and hence such files 
will be simply returned to the client as static files.  While it 
might be what you indeed tried to setup, the "doesn't simply work" 
suggests it isn't.  You may want to re-consider your configuration 
to ensure that requests to php files are properly proxied to 
appropriate backend servers.

The second location, which disables logging to some static files, 
looks correct, but it might have the same problem: as long as 
requests are handled in this location, some essential handling 
which was previously present might be missing, and this breaks 
things.  For example, a common error is to write something like 
this:

    location / {
        root /path/to/site;
    }

    location ~ \.css$ {
        # no root here
    }

Note that the root path is configured in "location /", but not in 
"location ~ \.css$", hence all css files will use some default 
root path (or the one inherited from previous configuration 
levels), which is likely incorrect.  An obvious fix would be to 
configure root at the server level instead, so it will be used for 
both locations.

Just in case, looking into error log usually makes such issues 
trivial to identify - nginx will complain if it cannot find a file 
requested, and will show full path it tried to use.

Hope this helps.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list