Bird
Raised Fist0
Nginxdevops~20 mins

Conditional logging in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of conditional logging in nginx?
easy
A. To log only specific requests based on conditions
B. To disable all logging permanently
C. To log every request without any filter
D. To log errors only, ignoring access logs

Solution

  1. Step 1: Understand logging basics in nginx

    Logging records requests to help monitor and debug web traffic.
  2. Step 2: Identify the role of conditional logging

    Conditional logging allows filtering which requests get logged based on rules.
  3. Final Answer:

    To log only specific requests based on conditions -> Option A
  4. Quick Check:

    Conditional logging = selective logging [OK]
Hint: Conditional logging means logging only some requests [OK]
Common Mistakes:
  • Thinking conditional logging disables all logs
  • Confusing conditional logging with error-only logging
  • Assuming it logs every request without filtering
2. Which of the following is the correct syntax to enable conditional logging in nginx using the access_log directive?
easy
A. access_log /var/log/nginx/access.log on_condition=$if;
B. access_log /var/log/nginx/access.log if=$condition;
C. access_log /var/log/nginx/access.log condition=$if;
D. access_log if=$condition /var/log/nginx/access.log;

Solution

  1. Step 1: Recall the correct order of parameters in access_log

    The syntax is: access_log <path> [format] [if=condition];
  2. Step 2: Identify the correct use of the if= option

    The condition must be specified as if=$variable after the log path.
  3. Final Answer:

    access_log /var/log/nginx/access.log if=$condition; -> Option B
  4. Quick Check:

    access_log path if=condition [OK]
Hint: Remember: if= comes after log file path in access_log [OK]
Common Mistakes:
  • Placing if= before the log file path
  • Using wrong parameter names like condition= or on_condition=
  • Omitting the $ sign before the variable
3. Given the following nginx configuration snippet, what will be the effect on logging?
map $request_uri $loggable {
    default 1;
    "/health" 0;
}

access_log /var/log/nginx/access.log combined if=$loggable;
medium
A. Only requests to /health will be logged
B. No requests will be logged
C. All requests except to /health will be logged
D. All requests will be logged regardless of URI

Solution

  1. Step 1: Understand the map directive

    The map sets $loggable to 0 for "/health" and 1 for all others.
  2. Step 2: Analyze the access_log condition

    Logging happens only if $loggable is true (1), so requests to "/health" (0) are skipped.
  3. Final Answer:

    All requests except to /health will be logged -> Option C
  4. Quick Check:

    map 0 disables logging for /health [OK]
Hint: map 0 disables logging; 1 enables it [OK]
Common Mistakes:
  • Assuming /health requests are logged
  • Thinking map disables all logging
  • Confusing default and specific URI values
4. Identify the error in this nginx configuration for conditional logging:
map $status $loggable {
    200 1;
    default 0;
}

access_log /var/log/nginx/access.log combined if=loggable;
medium
A. access_log path is invalid
B. map directive syntax is incorrect
C. Cannot use $status variable in map
D. Missing $ before loggable in access_log condition

Solution

  1. Step 1: Check variable usage in access_log

    Variables must be prefixed with $ in conditions, so if=loggable is wrong.
  2. Step 2: Confirm correct syntax

    Correct syntax is if=$loggable to reference the variable properly.
  3. Final Answer:

    Missing $ before loggable in access_log condition -> Option D
  4. Quick Check:

    Variables need $ prefix in if= [OK]
Hint: Always prefix variables with $ in if= conditions [OK]
Common Mistakes:
  • Omitting $ before variable in if= condition
  • Miswriting map syntax
  • Assuming $status cannot be used in map
5. You want to log all requests except those with user agent containing "Googlebot". Which configuration correctly implements this conditional logging?
hard
A. map $http_user_agent $loggable { default 1; ~Googlebot 0; } access_log /var/log/nginx/access.log combined if=$loggable;
B. map $http_user_agent $loggable { default 0; ~Googlebot 1; } access_log /var/log/nginx/access.log combined if=$loggable;
C. map $http_user_agent $loggable { default 1; Googlebot 0; } access_log /var/log/nginx/access.log combined if=$loggable;
D. map $http_user_agent $loggable { default 1; ~Googlebot 1; } access_log /var/log/nginx/access.log combined if=$loggable;

Solution

  1. Step 1: Use map with regex to detect "Googlebot" in user agent

    The ~ prefix allows regex matching; setting 0 disables logging for matching agents.
  2. Step 2: Set default to 1 to log all other requests

    Default 1 means log unless user agent matches Googlebot.
  3. Step 3: Use if=$loggable in access_log to apply condition

    This ensures only requests with $loggable=1 are logged.
  4. Final Answer:

    map $http_user_agent $loggable { default 1; ~Googlebot 0; } access_log /var/log/nginx/access.log combined if=$loggable; -> Option A
  5. Quick Check:

    Regex ~Googlebot disables logging for bots [OK]
Hint: Use ~ for regex in map to match user agents [OK]
Common Mistakes:
  • Using exact string without ~ for regex
  • Reversing default values causing wrong logging
  • Not prefixing variable with $ in if= condition