0
0
Nginxdevops~20 mins

Conditional logging in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the effect of this nginx config snippet?
Given this nginx configuration snippet, what will be the effect on logging?
map $status $loggable {
    ~^[23] 0;
    default 1;
}

access_log /var/log/nginx/access.log combined if=$loggable;
ALogging will be disabled completely.
BAll requests will be logged regardless of status code.
COnly requests with status codes starting with 2 or 3 will be logged; others will be ignored.
DRequests with status codes starting with 2 or 3 will NOT be logged; all others will be logged.
Attempts:
2 left
💡 Hint
Look at the map directive and the if condition in access_log.
Configuration
intermediate
2:00remaining
Choose the correct nginx config to log only client errors (4xx)
Which nginx configuration snippet will log only requests with client error status codes (4xx)?
A
map $status $log_client_error {
    default 1;
}
access_log /var/log/nginx/client_error.log combined if=$log_client_error;
B
map $status $log_client_error {
    ~^4 1;
    default 0;
}
access_log /var/log/nginx/client_error.log combined if=$log_client_error;
C
map $status $log_client_error {
    ~^[23] 1;
    default 0;
}
access_log /var/log/nginx/client_error.log combined if=$log_client_error;
D
map $status $log_client_error {
    ~^5 1;
    default 0;
}
access_log /var/log/nginx/client_error.log combined if=$log_client_error;
Attempts:
2 left
💡 Hint
Client errors have status codes starting with 4.
Troubleshoot
advanced
2:00remaining
Why does conditional logging not work as expected?
An nginx config uses:
map $status $loggable {
    ~^2 0;
    default 1;
}
access_log /var/log/nginx/access.log combined if=$loggable;

But logs still include 2xx responses. What is the likely cause?
AThe variable $status is not available at the time of logging evaluation.
BThe regex ~^2 matches only status code '2', not '200' or '201'.
CThe access_log directive does not support the 'if' parameter.
DThe map directive syntax is incorrect and causes a fallback to logging all.
Attempts:
2 left
💡 Hint
Consider when $status variable is set during request processing.
🔀 Workflow
advanced
2:30remaining
Order the steps to enable conditional logging for error responses only
Put these steps in the correct order to configure nginx to log only error responses (status 4xx and 5xx):
A1,4,2,3
B4,1,2,3
C1,2,4,3
D4,2,1,3
Attempts:
2 left
💡 Hint
Start by defining the variable, then specify patterns, then use it, then reload.
Best Practice
expert
3:00remaining
Which approach is best for conditional logging of slow requests in nginx?
You want to log only requests that take longer than 2 seconds to process. Which nginx configuration approach is best?
AUse the 'if' parameter in access_log with a variable set by a map on $request_time with a numeric comparison (e.g., $request_time > 2).
BUse the 'if' parameter in access_log with a variable set by a map on $request_time > 2.
CUse the 'if' parameter in access_log with a variable set by a map on $request_time matching a regex for values > 2.
DUse the 'if' parameter in access_log with a variable set by a map on $request_time with a threshold using the 'geo' module.
Attempts:
2 left
💡 Hint
Nginx map works with string matching, not numeric comparisons.