Bird
Raised Fist0
Nginxdevops~10 mins

Error log configuration in Nginx - Step-by-Step Execution

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
Process Flow - Error log configuration
Start nginx
Check error_log directive
Open error log file
Set log level
Write errors to log
Continue serving requests
On error, append entry to log file
Nginx reads the error_log directive, opens the specified file, sets the log level, and writes error messages there during runtime.
Execution Sample
Nginx
error_log /var/log/nginx/error.log warn;

server {
    listen 80;
    server_name example.com;
}
Configures nginx to log warnings and above to /var/log/nginx/error.log
Process Table
StepActionDirective EvaluatedLog FileLog LevelResult
1Start nginxN/AN/AN/ANginx process starts
2Read configerror_log /var/log/nginx/error.log warn;/var/log/nginx/error.logwarnLog file and level set
3Open log fileerror_log directive/var/log/nginx/error.logwarnFile opened for appending
4Serve requestsN/A/var/log/nginx/error.logwarnNormal operation
5Error occursN/A/var/log/nginx/error.logwarnError message written to log
6Stop nginxN/A/var/log/nginx/error.logwarnLog file closed
7ExitN/AN/AN/ANginx stopped, logging ended
💡 Nginx stops or reloads, closing log file and ending logging
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
log_fileundefined/var/log/nginx/error.log/var/log/nginx/error.log (opened)/var/log/nginx/error.log (written)closed
log_levelundefinedwarnwarnwarnwarn
Key Moments - 3 Insights
Why does nginx need the error_log directive before starting to log errors?
Because as shown in execution_table step 2, nginx reads the error_log directive to know which file to open and what level of errors to log before it can write any error messages.
What happens if the log file path is incorrect or not writable?
Nginx will fail to open the log file at step 3, which can cause errors or prevent logging. This is why the file path must be valid and writable.
Does nginx log all errors regardless of the log level set?
No, only errors at or above the configured log level (e.g., warn) are logged, as shown in step 5 where only warnings and more severe messages are written.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does nginx set as the log level?
Aerror
Bwarn
Cinfo
Ddebug
💡 Hint
Check the 'Log Level' column at step 2 in the execution_table.
At which step does nginx open the error log file for writing?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the step where the 'Result' says 'File opened for appending' in the execution_table.
If the log level is changed to 'error', how would the logging behavior change?
AFewer messages would be logged
BMore messages would be logged
CNo messages would be logged
DAll messages including debug would be logged
💡 Hint
Refer to the key_moments explanation about log level filtering.
Concept Snapshot
error_log <file_path> <level>;
- Sets where nginx writes error messages
- Levels: debug, info, notice, warn, error, crit, alert, emerg
- Only messages at or above level are logged
- Must be set before nginx starts logging
- Example: error_log /var/log/nginx/error.log warn;
Full Transcript
This visual execution shows how nginx configures error logging. When nginx starts, it reads the error_log directive to find the log file path and log level. It opens the file for appending error messages. During operation, errors at or above the set level are written to this file. If the file path is invalid, logging fails. The log level controls which errors are recorded, filtering out less severe messages. When nginx stops, it closes the log file and ends logging.

Practice

(1/5)
1. What is the main purpose of the error_log directive in nginx?
easy
A. To specify the file where error messages are recorded
B. To set the maximum number of client connections
C. To configure the server's IP address
D. To define the root directory for website files

Solution

  1. Step 1: Understand the role of error logs

    Error logs record problems and errors that happen in the server, helping to find and fix issues.
  2. Step 2: Identify what error_log does

    The error_log directive tells nginx where to save these error messages, specifying the file path and log level.
  3. Final Answer:

    To specify the file where error messages are recorded -> Option A
  4. Quick Check:

    error_log = file for errors [OK]
Hint: Error logs = where nginx saves error messages [OK]
Common Mistakes:
  • Confusing error_log with access_log
  • Thinking error_log sets server IP
  • Mixing error_log with client connection limits
2. Which of the following is the correct syntax to set the error log file to /var/log/nginx/error.log with log level warn?
easy
A. error_log /var/log/nginx/error.log level warn;
B. error_log = /var/log/nginx/error.log warn;
C. error_log /var/log/nginx/error.log warn
D. error_log /var/log/nginx/error.log warn;

Solution

  1. Step 1: Recall nginx error_log syntax

    The correct syntax is: error_log <file_path> <log_level>;
  2. Step 2: Check each option

    error_log /var/log/nginx/error.log warn; matches the correct syntax with semicolon and no extra symbols. Options A and B have invalid syntax, and D misses the semicolon.
  3. Final Answer:

    error_log /var/log/nginx/error.log warn; -> Option D
  4. Quick Check:

    Correct syntax ends with semicolon [OK]
Hint: Syntax: error_log path level; ends with semicolon [OK]
Common Mistakes:
  • Omitting the semicolon at the end
  • Using '=' sign incorrectly
  • Adding extra words like 'level'
3. Given this nginx configuration snippet:
error_log /var/log/nginx/error.log error;

What level of messages will be logged?
medium
A. Errors and more severe messages
B. Only critical errors
C. All messages including debug
D. Only warnings and errors

Solution

  1. Step 1: Understand log levels hierarchy

    Log levels in nginx from least to most severe: debug, info, notice, warn, error, crit, alert, emerg.
  2. Step 2: Interpret 'error' level

    Setting level to 'error' logs error and all more severe messages like critical, alert, emergency.
  3. Final Answer:

    Errors and more severe messages -> Option A
  4. Quick Check:

    error level logs error and above [OK]
Hint: Log level logs that level and higher severity [OK]
Common Mistakes:
  • Thinking it logs only critical errors
  • Assuming warnings are included at error level
  • Confusing debug with error level
4. You set error_log /var/log/nginx/error.log warn; but no warnings appear in the log file. What is the most likely cause?
medium
A. Error logs only record errors, not warnings
B. The log level 'warn' does not exist in nginx
C. The log file path is incorrect or not writable
D. You must restart nginx to enable error logging

Solution

  1. Step 1: Check log file path and permissions

    If the path is wrong or nginx cannot write to the file, logs won't appear.
  2. Step 2: Validate log level and service status

    'warn' is a valid level, and nginx logs warnings. Restarting is usually needed only after config changes, but logging works immediately if path is correct.
  3. Final Answer:

    The log file path is incorrect or not writable -> Option C
  4. Quick Check:

    Writable log file needed for logs [OK]
Hint: Check file path and permissions first if logs missing [OK]
Common Mistakes:
  • Assuming 'warn' is invalid log level
  • Forgetting to check file permissions
  • Thinking restart always needed for logging
5. You want to log all error messages including debug info to /var/log/nginx/full_error.log but keep normal error logs at /var/log/nginx/error.log with level error. Which configuration achieves this?
hard
A. error_log /var/log/nginx/error.log error debug;\nerror_log /var/log/nginx/full_error.log debug;
B. error_log /var/log/nginx/error.log error;\nerror_log /var/log/nginx/full_error.log debug;
C. error_log /var/log/nginx/error.log error;\nerror_log /var/log/nginx/full_error.log error;
D. error_log /var/log/nginx/error.log;\nerror_log /var/log/nginx/full_error.log debug;

Solution

  1. Step 1: Understand multiple error_log directives

    nginx allows multiple error_log directives to log to different files with different levels.
  2. Step 2: Check each option for correct syntax and intent

    error_log /var/log/nginx/error.log error;\nerror_log /var/log/nginx/full_error.log debug; correctly sets error level for normal log and debug level for full log. error_log /var/log/nginx/error.log error debug;\nerror_log /var/log/nginx/full_error.log debug; has invalid combined levels. error_log /var/log/nginx/error.log error;\nerror_log /var/log/nginx/full_error.log error; logs both at error level. error_log /var/log/nginx/error.log;\nerror_log /var/log/nginx/full_error.log debug; misses level for first log.
  3. Final Answer:

    error_log /var/log/nginx/error.log error;\nerror_log /var/log/nginx/full_error.log debug; -> Option B
  4. Quick Check:

    Separate directives for different levels [OK]
Hint: Use two error_log lines with different levels [OK]
Common Mistakes:
  • Combining log levels in one directive incorrectly
  • Omitting log level in error_log
  • Using one file for both levels