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 thelocationblock, 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
| Directive | Description |
|---|---|
| access_log off; | Disables access logging for the current context |
| location /path/ { ... } | Matches requests to /path/ |
| nginx -t | Tests Nginx configuration syntax |
| nginx -s reload | Reloads 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.