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
Log Format Customization in Nginx
📖 Scenario: You are managing a web server using Nginx. You want to customize the log format to include specific details about each request for better monitoring and troubleshooting.
🎯 Goal: Learn how to create a custom log format in Nginx and apply it to the access logs.
📋 What You'll Learn
Create a custom log format named custom_format with specific fields
Set a variable for the log file path
Apply the custom log format to the access log directive
Print the final access log configuration line
💡 Why This Matters
🌍 Real World
Customizing log formats helps system administrators and DevOps engineers monitor web traffic and troubleshoot issues effectively by capturing exactly the information they need.
💼 Career
Understanding how to customize and manage Nginx logs is a key skill for roles like DevOps engineer, system administrator, and site reliability engineer.
Progress0 / 4 steps
1
Create a custom log format
Write a log_format directive named custom_format that logs the client IP ($remote_addr), the request time ($time_local), the request method ($request_method), the requested URI ($request_uri), the response status ($status), and the body bytes sent ($body_bytes_sent).
Nginx
Hint
Use the log_format directive with the name custom_format and include the variables exactly as shown.
2
Set the access log file path variable
Create a variable called access_log_path and set it to the string "/var/log/nginx/access_custom.log".
Nginx
Hint
Use the set directive to assign the log file path to the variable $access_log_path.
3
Apply the custom log format to the access log
Write an access_log directive that uses the variable $access_log_path as the log file path and the custom_format as the log format.
Nginx
Hint
Use the access_log directive with the variable $access_log_path and the format name custom_format.
4
Display the final access log configuration
Write a print statement that outputs the exact string: access_log /var/log/nginx/access_custom.log custom_format;
Nginx
Hint
Use print with the exact string including the semicolon.
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
Step 1: Understand the role of log_format
The log_format directive lets you create a custom pattern for how nginx records access logs.
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.
Final Answer:
To define a custom format for access logs -> Option A
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
Step 1: Recall correct log_format syntax
The correct syntax uses log_format name 'format_string'; with single quotes around variables.
Step 2: Identify correct option
log_format myformat '$remote_addr $request_uri'; matches this syntax exactly. Other options use invalid braces, equals, or colons.
Final Answer:
log_format myformat '$remote_addr $request_uri'; -> Option B
Quick Check:
Correct syntax uses single quotes and semicolon [OK]
Hint: Use single quotes and semicolon for log_format [OK]
The format is: $remote_addr - $remote_user [$time_local] "$request" $status. This means client IP, dash, user, time, request, and status code in order.
Step 2: Match format to options
Client IP - user [time] "request" status code correctly describes the order and content of the log entry.
Final Answer:
Client IP - user [time] "request" status code -> Option C
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
Step 1: Check syntax requirements for log_format
The log_format directive must end with a semicolon (;).
Step 2: Identify the error in given config
The string lacks the terminating semicolon, causing a syntax error.
Final Answer:
Missing semicolon at the end -> Option A
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
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.
Step 2: Apply the custom format in access_log
To activate the format, use access_log with the format name and log file path.