Introduction
When a website becomes unavailable, returns unexpected HTTP errors, or behaves inconsistently, one of the first places to investigate is the web server's error log. For applications running behind Nginx, these logs provide valuable information about what the server is experiencing when handling incoming requests.
Nginx error logs record events such as failed upstream connections, permission problems, missing files, configuration errors, and resource limitations. Knowing how to locate and interpret these messages can significantly reduce the time spent diagnosing production issues.
In this guide, we'll look at where Nginx stores its error logs, how to inspect them using standard Linux commands, how to filter relevant messages, and how to use log entries to investigate common problems.
Where Are Nginx Error Logs Stored?
On most Linux installations, the default Nginx error log is located at:
/var/log/nginx/error.log
The access log is usually stored alongside it:
/var/log/nginx/access.log
These are common defaults on distributions such as Ubuntu and Debian, although the actual locations depend on the Nginx configuration and how the package was installed.
To check which error log file Nginx is configured to use, inspect its configuration:
sudo nginx -T | grep -i error_log
The nginx -T command tests and dumps the complete active configuration, including configuration files loaded through included directives. This makes it useful when the error log directive is defined in a separate virtual host or included configuration file.
Example output:
error_log /var/log/nginx/error.log warn;
This tells us that Nginx writes error messages to /var/log/nginx/error.log and uses the warn logging level.
Keep in mind that Nginx can have different error log destinations at different configuration levels. A server or location block may define its own error log, so inspecting the complete configuration is more reliable than checking only the main configuration file.
Viewing Nginx Error Logs
Linux provides several commands for inspecting log files. The most useful ones for day-to-day troubleshooting are tail, less, and grep.
1. View the Most Recent Log Entries
To display the last 50 lines of the Nginx error log, use:
sudo tail -n 50 /var/log/nginx/error.log
This is often the quickest way to identify a recent problem without reading the entire log file.
You can increase the number of lines if you need more context:
sudo tail -n 200 /var/log/nginx/error.log
2. Follow the Log in Real Time
When reproducing an issue, following the error log in real time is particularly useful:
sudo tail -f /var/log/nginx/error.log
New log entries will appear as Nginx writes them. This allows you to trigger a request in your browser or with curl and immediately inspect the resulting server messages.
For example, in one terminal window:
sudo tail -f /var/log/nginx/error.log
In another terminal, make a request to the affected website:
curl -I https://example.com
If the request causes an error that Nginx records, you should see a corresponding entry in the log.
Press Ctrl + C to stop following the file.
3. Read the Log with Less
For investigating older entries, less is more convenient than printing the entire file to the terminal:
sudo less /var/log/nginx/error.log
Useful keyboard shortcuts include:
G- Jump to the end of the file.g- Jump to the beginning./pattern- Search for a specific word or phrase.n- Move to the next search result.N- Move to the previous search result.q- Exit.
Filtering Nginx Error Logs
Production servers can generate a substantial amount of log output. Filtering the error log helps narrow an investigation down to relevant events.
Search for Specific Errors
The grep command can search for a particular word or phrase:
sudo grep -i "error" /var/log/nginx/error.log
The -i option makes the search case-insensitive.
You can also search for common Nginx error messages:
sudo grep -i "permission denied" /var/log/nginx/error.log
sudo grep -i "connection refused" /var/log/nginx/error.log
sudo grep -i "upstream timed out" /var/log/nginx/error.log
Combine Grep with Tail
To follow the log while displaying only entries matching a particular pattern, combine tail and grep:
sudo tail -f /var/log/nginx/error.log | grep --line-buffered -i "upstream"
The --line-buffered option helps ensure matching lines are displayed as they arrive, rather than being held in a buffer.
Search for Errors Within a Time Period
Nginx error log entries typically include timestamps. You can use these timestamps to narrow your investigation to a particular date or time.
For example, to search for entries containing a specific date:
sudo grep "2026/12/01" /var/log/nginx/error.log
Replace the date with the date you are investigating. The exact timestamp format depends on the log format and configuration.
Understanding Nginx Error Log Messages
Being able to read the log is only part of the process. Understanding what the messages mean is what helps you identify the underlying problem.
A typical Nginx error log entry looks similar to this:
2026/12/01 10:15:32 [error] 1234#1234: *56 connect() failed (111: Connection refused) while connecting to upstream, client: 192.0.2.10, server: example.com, request: "GET /api/users HTTP/1.1", upstream: "http://127.0.0.1:8000/api/users", host: "example.com"
Let's break down the important parts:
- Timestamp:
2026/12/01 10:15:32indicates when the event occurred. - Severity:
[error]identifies the logging level. - Process and connection identifiers:
1234#1234and*56help identify the worker process and connection involved. - Error description:
connect() failed (111: Connection refused)describes the failure. - Client: The IP address of the client making the request.
- Server: The Nginx server block handling the request.
- Request: The HTTP method, path, and protocol.
- Upstream: The backend address Nginx was attempting to contact.
This information can help you determine whether a problem originates in Nginx itself, the application behind it, or the connection between the two.
Common Nginx Errors and How to Investigate Them
1. Connection Refused
Example:
connect() failed (111: Connection refused) while connecting to upstream
This usually means Nginx attempted to connect to an upstream service, but the connection was actively refused.
Possible causes include:
- The application process is not running.
- The application is listening on a different port.
- The upstream address is incorrect.
- The application is bound to a different network interface.
- A container or service dependency is unavailable.
Start by checking whether the application is running:
sudo systemctl status php8.3-fpm
Replace the service name with the actual service used by your application.
Next, check whether the expected port is listening:
sudo ss -lntp
If the application is expected to listen on port 8000, you can filter the output:
sudo ss -lntp | grep ':8000'
For containerised applications, also check the container status and its logs:
docker ps
docker logs <container_name>
2. Permission Denied
Example:
open() "/var/www/html/index.html" failed (13: Permission denied)
This indicates that Nginx attempted to access a file or directory but did not have the necessary permissions.
Common causes include:
- Incorrect ownership of application files.
- File permissions preventing the Nginx worker process from reading content.
- Missing execute permissions on one of the parent directories.
- Security restrictions imposed by SELinux or AppArmor.
Check the permissions of the affected file and its parent directories:
ls -l /var/www/html/index.html
namei -l /var/www/html/index.html
The namei command is particularly useful because it shows permissions along the entire directory path.
Do not immediately resolve permission errors by applying broad permissions such as chmod -R 777. Instead, identify which user Nginx runs as and grant only the access required.
3. Upstream Timed Out
Example:
upstream timed out (110: Connection timed out) while reading response header from upstream
This means Nginx did not receive the expected response from the upstream service within the configured timeout.
Possible causes include:
- Slow application requests.
- Database queries taking too long.
- Insufficient application worker capacity.
- An overloaded backend server.
- Network connectivity problems between Nginx and the upstream.
Start by inspecting the application logs at the same timestamp. If the application uses PHP-FPM, check its service logs:
sudo journalctl -u php8.3-fpm --since "10 minutes ago"
You can also inspect system resource usage:
top
free -h
df -h
Increasing Nginx timeout values may sometimes be appropriate, but it should not be the first response. A timeout can be a symptom of a slow or unhealthy backend, and increasing the limit may simply make requests wait longer.
4. No Such File or Directory
Example:
open() "/var/www/html/assets/app.css" failed (2: No such file or directory)
This means Nginx attempted to access a file that does not exist at the resolved path.
Check the relevant root or alias directive in the server configuration, then verify that the requested file exists:
ls -l /var/www/html/assets/app.css
For applications using a deployment pipeline, check whether the latest release contains the expected assets and whether Nginx is pointing at the correct release directory.
Understanding Nginx Logging Levels
Nginx supports several error log severity levels. These determine which messages are written to the configured error log.
| Level | Description |
|---|---|
debug |
Detailed diagnostic information, useful for investigating complex issues. |
info |
Informational messages about server activity. |
notice |
Normal but significant events. |
warn |
Warning messages that may indicate a potential problem. |
error |
Error conditions that require attention. |
crit |
Critical conditions. |
alert |
Conditions requiring immediate action. |
emerg |
Emergency conditions where the system may be unusable. |
For example, the following directive sets the error log level to warn:
error_log /var/log/nginx/error.log warn;
Changing the level to debug can provide more detailed information when investigating difficult problems:
error_log /var/log/nginx/error.log debug;
However, debug logging can generate a large amount of output and may affect performance. It also requires an Nginx build compiled with the debug support option. Use it temporarily and only when necessary.
Checking Logs Through systemd
Depending on how Nginx was installed and configured, some service messages may also be available through the systemd journal.
To inspect recent Nginx service logs:
sudo journalctl -u nginx -n 100
To follow new journal entries in real time:
sudo journalctl -u nginx -f
To inspect messages from a particular period:
sudo journalctl -u nginx --since "1 hour ago"
The journal is useful for investigating service startup failures, configuration problems, and events recorded by the service manager. It does not necessarily contain every request-level error recorded in Nginx's configured error log.
Managing Log Rotation
Nginx error logs can grow over time, especially on busy production servers. Without log rotation, they can consume a significant amount of disk space.
Many Linux package installations configure log rotation through logrotate.
Check whether an Nginx log rotation configuration exists:
cat /etc/logrotate.d/nginx
A typical configuration may look similar to this:
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
if [ -f /run/nginx.pid ]; then
kill -USR1 $(cat /run/nginx.pid)
fi
endscript
}
This example rotates logs daily, keeps 14 rotations, compresses older files, and signals Nginx to reopen its log files after rotation.
The exact configuration can vary by distribution. The important point is that Nginx needs to reopen its log files after rotation so it does not continue writing to an old file descriptor.
A Practical Nginx Troubleshooting Workflow
When investigating a production issue, it helps to follow a consistent process rather than immediately changing configuration settings.
- Reproduce the issue: Make a request to the affected URL and record the approximate time.
- Inspect the error log: Use
tail -fortail -n 100to look for new messages. - Identify the failure: Look for connection errors, permission problems, missing files, or upstream timeouts.
- Check the relevant service: Inspect Nginx, PHP-FPM, application, database, or container logs as appropriate.
- Verify the underlying cause: Check connectivity, permissions, ports, resource usage, and application health.
- Apply a targeted fix: Change only what is necessary to resolve the issue.
- Retest: Repeat the original request and confirm that the error no longer appears.
For example, if a website returns a 502 Bad Gateway response, the Nginx error log may reveal that the upstream connection was refused. Rather than changing the Nginx configuration immediately, investigate whether the application service is running and listening on the expected address and port.
Conclusion
Nginx error logs are one of the most useful tools available when diagnosing web server problems. They provide insight into failed upstream connections, permission issues, missing files, timeout conditions, and other events that can affect application availability.
Commands such as tail, less, grep, and journalctl make it straightforward to inspect log output, investigate recent failures, and monitor server behaviour while reproducing an issue.
For DevOps engineers, regularly reviewing server logs and understanding what common error messages mean is an important part of maintaining reliable web infrastructure. Rather than treating every error as an Nginx configuration problem, use the information in the logs to trace failures back to their actual source.
Useful Commands Reference
| Command | Purpose |
|---|---|
sudo tail -n 50 /var/log/nginx/error.log |
View the latest 50 error log entries. |
sudo tail -f /var/log/nginx/error.log |
Follow the error log in real time. |
sudo less /var/log/nginx/error.log |
Browse the log interactively. |
sudo grep -i "error" /var/log/nginx/error.log |
Search for matching error messages. |
sudo nginx -T |
Test and dump the active Nginx configuration. |
sudo nginx -t |
Test the Nginx configuration syntax. |
sudo journalctl -u nginx -f |
Follow Nginx service messages in systemd. |
sudo ss -lntp |
Inspect listening TCP ports and processes. |