Bird
Raised Fist0
Nginxdevops~20 mins

Log format customization 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
🎖️
Log Format Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this custom log format directive?
Given the following nginx log_format directive, what will be the logged output for a request with remote_addr 192.168.1.10, request_method GET, and request_uri /home?
Nginx
log_format custom '$remote_addr - $request_method "$request_uri"';
access_log /var/log/nginx/access.log custom;
A192.168.1.10 GET /home
B192.168.1.10 - GET /home
C192.168.1.10 - GET "/home"
D192.168.1.10 - "GET" "/home"
Attempts:
2 left
💡 Hint
Look carefully at the quotes and dashes in the log_format string.
Configuration
intermediate
2:00remaining
Which log_format directive correctly logs the request time in seconds with millisecond precision?
You want to log the request processing time in seconds with millisecond precision using the variable $request_time. Which of the following log_format directives achieves this?
Alog_format timed '$remote_addr - $request_time';
Blog_format timed '$remote_addr - $request_time ms';
Clog_format timed '$remote_addr - $request_time seconds';
Dlog_format timed '$remote_addr - $request_time'; # $request_time is in seconds with milliseconds
Attempts:
2 left
💡 Hint
Remember that $request_time already includes seconds with milliseconds as a decimal number.
Troubleshoot
advanced
2:00remaining
Why does this custom log format cause nginx to fail to start?
Consider this nginx configuration snippet: log_format broken '$remote_addr - $request_method $request_uri; access_log /var/log/nginx/access.log broken; Why does nginx fail to start with this configuration?
Nginx
log_format broken '$remote_addr - $request_method $request_uri;
access_log /var/log/nginx/access.log broken;
AThe variable $request_uri is invalid in log_format and causes failure.
BMissing closing single quote in the log_format directive causes a syntax error.
CThe access_log path is incorrect and prevents nginx from starting.
DThe log_format name 'broken' is a reserved keyword and cannot be used.
Attempts:
2 left
💡 Hint
Check the quotes carefully in the log_format directive.
🔀 Workflow
advanced
2:00remaining
What is the correct sequence to apply a new custom log format without downtime?
You created a new log_format named 'detailed' in nginx.conf. What is the correct workflow to apply this change without dropping active connections?
A3,1,2,4
B1,3,2,4
C3,2,1,4
D1,2,3,4
Attempts:
2 left
💡 Hint
You must edit config before testing syntax.
Best Practice
expert
2:00remaining
Which custom log format best balances detailed info and log file size?
You want a custom nginx log format that logs client IP, request method, URI, status code, and request time, but avoids logging headers or cookies to keep log size manageable. Which option is best?
Alog_format balanced '$remote_addr - $request_method "$request_uri" $status $request_time';
Blog_format balanced '$remote_addr - $request_method "$request_uri" $status $request_time "$http_user_agent" "$http_cookie"';
Clog_format balanced '$remote_addr - $request_method "$request_uri" $status $request_time $request_length';
Dlog_format balanced '$remote_addr - $request_method "$request_uri" $status $request_time "$http_referer"';
Attempts:
2 left
💡 Hint
Avoid logging headers or cookies to reduce log size.

Practice

(1/5)
1. What is the purpose of the log_format directive in nginx?
easy
A. To define a custom format for access logs
B. To specify the location of error logs
C. To enable SSL encryption for logs
D. To set the maximum size of log files

Solution

  1. Step 1: Understand the role of log_format

    The log_format directive lets you create a custom pattern for how nginx records access logs.
  2. Step 2: Differentiate from other logging directives

    Other directives like error_log set error log location, not format. log_format is specifically for access log formatting.
  3. Final Answer:

    To define a custom format for access logs -> Option A
  4. Quick Check:

    log_format = custom access log format [OK]
Hint: log_format sets how access logs look, not location [OK]
Common Mistakes:
  • Confusing log_format with error_log
  • Thinking log_format sets log file size
  • Assuming log_format enables encryption
2. Which of the following is the correct syntax to define a custom log format named myformat that logs the client IP and request URI?
easy
A. log_format myformat = '$remote_addr $request_uri';
B. log_format myformat '$remote_addr $request_uri';
C. log_format myformat { $remote_addr $request_uri };
D. log_format myformat: '$remote_addr $request_uri';

Solution

  1. Step 1: Recall correct log_format syntax

    The correct syntax uses log_format name 'format_string'; with single quotes around variables.
  2. Step 2: Identify correct option

    log_format myformat '$remote_addr $request_uri'; matches this syntax exactly. Other options use invalid braces, equals, or colons.
  3. Final Answer:

    log_format myformat '$remote_addr $request_uri'; -> Option B
  4. Quick Check:

    Correct syntax uses single quotes and semicolon [OK]
Hint: Use single quotes and semicolon for log_format [OK]
Common Mistakes:
  • Using braces {} instead of quotes
  • Adding equals sign = incorrectly
  • Using colon : after format name
3. Given this nginx configuration snippet:
log_format custom '$remote_addr - $remote_user [$time_local] "$request" $status';
access_log /var/log/nginx/access.log custom;

What will be the output format of each log entry?
medium
A. Status code - Client IP [time] "request" user
B. User - Client IP [time] "request" status code
C. Client IP - user [time] "request" status code
D. Request - Client IP [time] "user" status code

Solution

  1. Step 1: Analyze the log_format string

    The format is: $remote_addr - $remote_user [$time_local] "$request" $status. This means client IP, dash, user, time, request, and status code in order.
  2. Step 2: Match format to options

    Client IP - user [time] "request" status code correctly describes the order and content of the log entry.
  3. Final Answer:

    Client IP - user [time] "request" status code -> Option C
  4. Quick Check:

    Variables order matches Client IP - user [time] "request" status code [OK]
Hint: Match variables order exactly to format string [OK]
Common Mistakes:
  • Mixing order of variables
  • Confusing $remote_user with $remote_addr
  • Ignoring quotes around $request
4. You wrote this configuration:
log_format mylog $remote_addr - $request_uri

But nginx fails to start. What is the error?
medium
A. Missing semicolon at the end
B. Missing quotes around the log format string
C. Incorrect variable name $request_uri
D. log_format directive cannot be used in http block

Solution

  1. Step 1: Check syntax requirements for log_format

    The log_format directive must end with a semicolon (;).
  2. Step 2: Identify the error in given config

    The string lacks the terminating semicolon, causing a syntax error.
  3. Final Answer:

    Missing semicolon at the end -> Option A
  4. Quick Check:

    Semicolon terminates the directive [OK]
Hint: Every nginx directive ends with semicolon [OK]
Common Mistakes:
  • Forgetting quotes around format string
  • Using wrong variable names
  • Omitting semicolon
5. You want to log the client IP, request method, and response time in seconds with 3 decimal places. Which custom log_format definition achieves this?
hard
A. log_format timed '$remote_addr $request_method $request_time'; access_log /var/log/nginx/timed.log timed if=$request_time;
B. log_format timed '$remote_addr $request_method $request_time sec';
C. log_format timed '$remote_addr $request_method $request_time';
D. log_format timed '$remote_addr $request_method $request_time'; access_log /var/log/nginx/timed.log timed;

Solution

  1. Step 1: Define log_format with required variables

    Use $remote_addr for client IP, $request_method for method, and $request_time for response time with decimals.
  2. Step 2: Apply the custom format in access_log

    To activate the format, use access_log with the format name and log file path.
  3. Final Answer:

    log_format timed '$remote_addr $request_method $request_time'; access_log /var/log/nginx/timed.log timed; -> Option D
  4. Quick Check:

    Define format and apply with access_log [OK]
Hint: Define format then apply with access_log [OK]
Common Mistakes:
  • Not applying log_format with access_log
  • Adding unnecessary text inside format
  • Using invalid if condition in access_log