How to Disable Nginx Access Logging for Specific URLs (Like Health Checks) While Keeping Other Logs


2 views

When running Nginx behind AWS ELB, health check pings to endpoints like /documents/ping can flood your access logs with repetitive entries. This creates noise in log analysis and increases storage costs without providing valuable information.

Here are three effective approaches to solve this:

1. Separate Location Block for Health Check

server {
    listen $PORT;
    
    location = /documents/ping {
        proxy_pass http://127.0.0.1:5000/ping;
        access_log off;
    }
    
    location ~ ^/documents/(.*)$ {
        proxy_pass http://127.0.0.1:5000/$1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

2. Conditional Logging with Map Directive

map $uri $loggable {
    /documents/ping 0;
    default 1;
}

server {
    listen $PORT;
    
    access_log /var/log/nginx/access.log combined if=$loggable;
    
    location ~ ^/documents/(.*)$ {
        proxy_pass http://127.0.0.1:5000/$1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

3. Log Filtering with Custom Log Format

log_format filtered '$remote_addr - $remote_user [$time_local] '
                   '"$request" $status $body_bytes_sent '
                   '"$http_referer" "$http_user_agent" '
                   '$request_time';
                   
server {
    listen $PORT;
    
    set $nolog 0;
    
    location ~ ^/documents/(.*)$ {
        if ($uri = /documents/ping) {
            set $nolog 1;
        }
        proxy_pass http://127.0.0.1:5000/$1;
        access_log /var/log/nginx/access.log filtered if=$nolog;
    }
}

For ELB health checks specifically, consider these additional optimizations:

  • Use HTTP 204 (No Content) responses for ping endpoints
  • Set extremely short timeouts (e.g., 1s) for health check locations
  • Consider rate limiting if you need to maintain some ping logs but reduce volume

After implementing any solution:

sudo nginx -t
sudo systemctl reload nginx
tail -f /var/log/nginx/access.log | grep -v "/documents/ping"

This ensures your configuration is valid while verifying the exclusion of health check requests from logs.


When running Nginx behind AWS ELB, health check requests to endpoints like /documents/ping can flood your access logs unnecessarily. Each health check generates a log entry, which quickly becomes noise in monitoring systems like CloudWatch.

The most efficient way to handle this is by creating a specific location block for the ping endpoint and disabling logging:

server {
    listen $PORT;

    location = /documents/ping {
        proxy_pass http://127.0.0.1:5000/ping;
        access_log off;
    }

    location ~ ^/documents/(.*)$ {
        proxy_pass http://127.0.0.1:5000/$1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location = /favicon.ico {
        return 204;
        access_log off;
        log_not_found off;
    }
}

The exact match location (location =) takes precedence over regex matches. Nginx will:

  • First check for exact matches to /documents/ping
  • Fall back to the regex pattern for other document paths
  • Process logging independently for each location

For more complex scenarios, you can use map-based conditional logging:

map $request_uri $loggable {
    ~^/documents/ping  0;
    default            1;
}

server {
    # ...
    access_log /var/log/nginx/access.log combined if=$loggable;
}

While both solutions work, the location-based approach is more efficient because:

  • No additional variable evaluation is needed
  • Nginx's location matching is highly optimized
  • Configuration remains clean and self-documenting