Process Flow - Conditional logging
Request Received
Evaluate Condition
Log Request
Send Response
Nginx checks a condition for each request. If true, it logs the request; if false, it skips logging.
map $request_uri $loggable { default 1; ~^/health 0; } access_log /var/log/nginx/access.log combined if=$loggable;
| Step | Request URI | Condition ($loggable) | Action | Log Output |
|---|---|---|---|---|
| 1 | /index.html | 1 (true) | Log request | Logged /index.html |
| 2 | /health | 0 (false) | Skip logging | No log |
| 3 | /api/data | 1 (true) | Log request | Logged /api/data |
| 4 | /health/status | 0 (false) | Skip logging | No log |
| 5 | /about | 1 (true) | Log request | Logged /about |
| 6 | End of requests | - | - | - |
| Variable | Start | After 1 | After 2 | After 3 | After 4 | After 5 | Final |
|---|---|---|---|---|---|---|---|
| $request_uri | - | /index.html | /health | /api/data | /health/status | /about | - |
| $loggable | - | 1 | 0 | 1 | 0 | 1 | - |
Conditional logging in nginx uses variables and the 'if' parameter in access_log. Use 'map' to set a variable based on request properties. Set 'if=$variable' in access_log to log only when variable is true. Example: skip logging health checks by setting variable to 0 for /health URIs. This reduces log noise and improves log relevance.