Turn off access_log for specific path while still allowing fallthrough to PHP

mpen asked:

Here’s my config:

server {
    root /app/public;
    charset utf-8;
    server_tokens off;

    location / {
        try_files $uri @app;

    location = /php-health {
        access_log off;

    location = /nginx-health {
        access_log off;
        default_type text/plain;
        return 200 "Nginx OK";

    location @app {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /app/index.php;
        fastcgi_param PATH_INFO $fastcgi_path_info;

The problem is when I put that location = /php-health block in there, Nginx no longer forward the request to my PHP app via location @app. I thought that with the absence of a return statement it would "fall through" to @app but it’s just 404ing.

What’s the proper way to do this?

My answer:

There are probably more or less hacky ways to do this, but changing the access_log setting is one of the things you can safely do with if.

For example:

location @app {
    if ($uri = /php-health) {
        access_log off;
    #... the PHP directives ...

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.