Understanding Nginx Logging
Nginx is a powerful web server and reverse proxy often used in conjunction with application servers like Django (using FastCGI or other WSGI servers). When things go wrong – whether due to configuration errors, application bugs, or server issues – it’s crucial to have access to error logs to diagnose the problem. This tutorial explains how to find and interpret Nginx error logs.
How Nginx Logs Errors
Nginx logs errors separately from access requests. The error_log directive in your Nginx configuration file determines where these error messages are written. If no error_log directive is explicitly defined, Nginx defaults to logging errors to the standard error stream, which typically ends up in a system log file (like /var/log/syslog or /var/log/messages depending on your operating system). However, it’s best practice to configure a dedicated error log file for easier troubleshooting.
Finding Your Nginx Configuration File
The first step is to locate your Nginx configuration file (nginx.conf). The location varies depending on your operating system and installation method. Common locations include:
- /etc/nginx/nginx.conf
- /usr/local/etc/nginx/nginx.conf
- /opt/nginx/conf/nginx.conf
You can use the following command to quickly find the configuration file:
nginx -t
This command tests your configuration and also displays the path to the configuration file being used.
Inspecting the nginx.conf File
Once you’ve located nginx.conf, open it in a text editor. Search for the error_log directive. The directive typically looks like this:
error_log /var/log/nginx/error.log;
The path following error_log specifies the location of the error log file. The directive can also include a severity level:
error_log /var/log/nginx/error.log warn;
This example logs messages of warn severity or higher (including error, crit, alert, and emerg).
Common Log File Locations
If you don’t find an explicit error_log directive, or you suspect the configuration might be different, here are some common locations for Nginx error logs:
- /var/log/nginx/error.log (Most common on Linux distributions like Debian, Ubuntu, CentOS, and Fedora)
- /usr/local/var/log/nginx/error.log (Common on macOS when installed via Homebrew)
- /opt/nginx/logs/error.log (Common for installations in a dedicated
optdirectory) - /var/log/messages or /var/log/syslog (If no
error_logdirective is present, errors might be logged to the system log)
Using lsof to Locate Open Log Files
If you’re still unable to find the error log, you can use the lsof (List Open Files) command to identify which files Nginx has open. This can help you pinpoint the log file.
-
Find the Nginx Process ID (PID): Use
psandgrepto find the Nginx master process:ps aux | grep nginxLook for a line that includes
nginx: master process. The second column of that line is the PID. -
Use
lsofwith the PID: Replace<PID>with the actual process ID:lsof -p <PID> | grep logThis command will list all open files associated with the Nginx process, filtering for those containing "log" in the filename.
Monitoring the Log File
Once you’ve located the error log, you can monitor it in real-time using the tail command:
tail -f /var/log/nginx/error.log
This command displays the last few lines of the log file and continues to update as new errors are written. The -f option ("follow") ensures that tail keeps the file open and displays new content as it is appended.
Troubleshooting Permissions
If you’re unable to access the error log file, make sure you have the necessary permissions. You may need to use sudo to view or edit the file if you are not the owner or a member of the appropriate group.