Bird
Raised Fist0
Nginxdevops~5 mins

Error log configuration in Nginx - Commands & Configuration

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
Introduction
When a web server has problems, it writes messages to a file called an error log. Configuring this log helps you find and fix issues quickly by saving important error details.
When you want to record server errors to understand why a website is not working.
When you need to keep track of warnings or critical failures in your web server.
When you want to change where the error messages are saved for easier access.
When you want to control how much detail is saved in the error logs to avoid large files.
When troubleshooting slow or failing web pages to see what errors occur.
Config File - nginx.conf
nginx.conf
error_log /var/log/nginx/error.log warn;

http {
    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

error_log /var/log/nginx/error.log warn; sets the file path and minimum level of errors to log. Here, warnings and more serious messages are saved.

The http block contains server settings. Inside it, the server block defines a website listening on port 80 for example.com.

This configuration ensures error messages from this server are saved to the specified file with the chosen detail level.

Commands
This command tests the nginx configuration file for syntax errors before applying changes. It helps avoid server crashes due to bad config.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server, so your website stays online.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command shows the last 10 lines of the error log file so you can quickly see recent errors or warnings.
Terminal
tail -n 10 /var/log/nginx/error.log
Expected OutputExpected
No output (command runs silently)
-n 10 - Shows the last 10 lines of the file
Key Concept

If you remember nothing else from this pattern, remember: setting the error_log path and level controls where and how nginx records problems.

Common Mistakes
Not testing the nginx configuration with 'nginx -t' before reloading.
This can cause nginx to fail to reload if there are syntax errors, making the website unavailable.
Always run 'sudo nginx -t' to check for errors before reloading the server.
Setting the error log level too high, like 'debug', in production.
This creates very large log files and can slow down the server.
Use 'warn' or 'error' levels in production to balance detail and performance.
Not having write permission for the error log file location.
Nginx cannot write logs, so errors won't be recorded, making troubleshooting impossible.
Ensure the nginx user has write access to the error log directory and file.
Summary
Configure the error_log directive in nginx.conf to set log file path and error level.
Test the configuration with 'nginx -t' to avoid syntax errors.
Reload nginx with 'systemctl reload nginx' to apply changes without downtime.
Use 'tail' to view recent error log entries for troubleshooting.

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