0
0
Nginxdevops~10 mins

Conditional logging in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
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.
Execution Sample
Nginx
map $request_uri $loggable {
    default 1;
    ~^/health 0;
}

access_log /var/log/nginx/access.log combined if=$loggable;
This config logs all requests except those starting with /health.
Process Table
StepRequest URICondition ($loggable)ActionLog Output
1/index.html1 (true)Log requestLogged /index.html
2/health0 (false)Skip loggingNo log
3/api/data1 (true)Log requestLogged /api/data
4/health/status0 (false)Skip loggingNo log
5/about1 (true)Log requestLogged /about
6End of requests---
💡 All requests processed; logging done only if condition is true.
Status Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
$request_uri-/index.html/health/api/data/health/status/about-
$loggable-10101-
Key Moments - 2 Insights
Why does /health not get logged even though it is a valid request?
Because the condition $loggable is set to 0 for URIs starting with /health, as shown in rows 2 and 4 of the execution_table, so logging is skipped.
What does the 'if=$loggable' part do in the access_log directive?
It tells nginx to log the request only if $loggable is true (1). This is why requests with $loggable=0 are not logged, as seen in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $loggable for the request '/api/data'?
A0
B-
C1
DUndefined
💡 Hint
Check row 3 under the 'Condition ($loggable)' column in the execution_table.
At which step does the logging condition evaluate to false?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look for rows where 'Condition ($loggable)' is 0 in the execution_table.
If we remove the line '~^/health 0;' from the map block, what happens to logging for '/health' requests?
AThey will still not be logged
BThey will be logged
CLogging will stop for all requests
DNginx will error
💡 Hint
Refer to how the map sets default to 1 and the effect of the regex line in the variable_tracker.
Concept Snapshot
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.
Full Transcript
In nginx, conditional logging lets you decide which requests to log based on a condition. We use the 'map' directive to create a variable, here called $loggable, which is 1 for most requests but 0 for those starting with /health. Then, in the access_log directive, we add 'if=$loggable' so nginx logs only when $loggable is true. The execution table shows requests with /health are skipped from logging because their $loggable is 0. This helps keep logs clean by ignoring routine health checks. Beginners often wonder why some requests are not logged; it's because the condition variable controls logging. Removing the regex line in the map would cause all requests to be logged, including /health. This method is simple and effective for controlling nginx logs.