0
0
NginxHow-ToBeginner · 3 min read

Disable Logging for Specific Path in Nginx: Simple Guide

To disable logging for a specific path in Nginx, use the access_log off; directive inside the location block for that path. This stops Nginx from writing access logs for requests matching that path while keeping logging enabled elsewhere.
📐

Syntax

The access_log directive controls logging of client requests in Nginx. To disable logging for a specific path, place access_log off; inside the location block that matches the path.

Parts explained:

  • location /path/ { ... }: Defines the URL path to match.
  • access_log off;: Turns off access logging for requests matching this location.
nginx
location /example/ {
    access_log off;
    # other settings
}
💻

Example

This example disables logging for requests to /static/ while keeping logging enabled for all other requests.

nginx
http {
    access_log /var/log/nginx/access.log;

    server {
        listen 80;
        server_name example.com;

        location /static/ {
            access_log off;
            root /var/www/static;
        }

        location / {
            root /var/www/html;
        }
    }
}
Output
Requests to /static/ will not be logged in /var/log/nginx/access.log, but all other requests will be logged as usual.
⚠️

Common Pitfalls

Common mistakes when disabling logging for a specific path include:

  • Placing access_log off; outside the location block, which disables logging globally.
  • Using incorrect path matching, causing logging to remain enabled.
  • Not reloading Nginx after configuration changes.

Always test your configuration with nginx -t and reload with nginx -s reload.

nginx
## Wrong: disables logging globally
access_log off;

## Right: disables logging only for /api/
location /api/ {
    access_log off;
}
📊

Quick Reference

DirectiveDescription
access_log off;Disables access logging for the current context
location /path/ { ... }Matches requests to /path/
nginx -tTests Nginx configuration syntax
nginx -s reloadReloads Nginx to apply config changes

Key Takeaways

Use access_log off; inside a location block to disable logging for that path only.
Keep global logging enabled outside the specific location to log other requests.
Always test your Nginx config with nginx -t before reloading.
Reload Nginx after changes with nginx -s reload to apply new settings.
Incorrect placement of access_log off; can disable logging globally by mistake.