Sub topic 12
Troubleshooting common problems
This chapter assumes you can install Nginx, edit its configuration, and use systemctl and curl. It also assumes you know how server blocks, location blocks, and reverse proxying work, and where the access and error logs are. Here you will turn those pieces into a repeatable way to diagnose faults.
A method that works every time
Most Nginx problems fall into a few classes. Work through them in order and stop at the first check that fails.
- Does the configuration parse? Run
sudo nginx -t. - Is the service running? Run
systemctl status nginx. - Is something listening on the expected port? Run
sudo ss -tlnp. - Does the request reach Nginx? Run
curl -I http://localhostand read the status code. - What does the error log say? Run
sudo tail -n 50 /var/log/nginx/error.log.
The status code tells you where to look next. The table below maps the codes you will see most often to their usual cause.
| Status | Usual cause | First place to look |
|---|---|---|
| 403 | Permissions on files or directories, or no index file and autoindex is off | Error log, file ownership, and execute permission on every parent directory |
| 404 | Wrong `root` or `location`, or the file does not exist | `root` path and `try_files` |
| 502 | Backend is down, refused the connection, or sent a broken reply | Backend service status and the `proxy_pass` address |
| 504 | Backend did not answer in time | Backend load and `proxy_read_timeout` |
| 301/302 loop | Redirect rules that send the client back to itself | `return` and `rewrite` rules, and proxy headers |
Configuration errors
Always test before you reload. The command prints the file and line number of the first error it finds.
sudo nginx -t && sudo systemctl reload nginxTypical messages and their meaning:
unexpected "}"orunexpected end of file: a missing semicolon or an unbalanced brace earlier in the file.unknown directive "...": a typo, or a directive from a module that is not compiled in. Runnginx -Vto see the compiled modules."server_name" directive is not allowed here: the directive is in the wrong context, for example insidehttpbut outsideserver.duplicate location "/": two location blocks have the same match in the same server.conflicting server name "example.com" on 0.0.0.0:80, ignored: this is a warning. Two server blocks claim the same name and port, and Nginx ignores the second one.
To see the configuration as Nginx actually understands it, with all include files merged, use the dump option.
sudo nginx -T | lessThis is the quickest way to find a stray file in conf.d or sites-enabled that overrides what you edited.
The service will not start
If systemctl status nginx shows failed, the reason is usually in the journal.
sudo journalctl -u nginx --no-pager -n 30The most common cause is a port conflict:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)Find the process that holds the port.
sudo ss -tlnp | grep ':80 'Another web server such as Apache is often the culprit. Stop it, or change its port, and then start Nginx. If the error is 13: Permission denied, you are binding to a port below 1024 without root privileges, or SELinux is blocking the port. On SELinux systems, sudo ausearch -m avc -ts recent shows the denial.
A reload that appears to do nothing is a separate problem. If the master process cannot parse the new configuration, it keeps running with the old one. Always run the test first and check the exit code.
403 Forbidden
A 403 almost always comes from the file system. The error log gives the precise reason:
open() "/var/www/example.com/html/index.html" failed (13: Permission denied)
directory index of "/var/www/example.com/html/" is forbiddenThe first line means the worker process user cannot read the file. The second means there is no index file and directory listing is off.
Check the worker user and the permissions along the whole path. Every parent directory needs the execute bit for that user, not only the final directory.
grep '^user' /etc/nginx/nginx.conf
namei -l /var/www/example.com/html/index.htmlThe namei -l command lists the owner and mode of every component of the path. A restrictive home directory, such as /home/alice with mode 700, is a frequent cause when the site root lives under it.
A fix that is usually correct:
sudo chown -R www-data:www-data /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} +
sudo find /var/www/example.com -type f -exec chmod 644 {} +On SELinux systems, file labels matter as well. Files must carry the httpd_sys_content_t type, which restorecon -Rv /var/www restores.
404 Not Found
A 404 means Nginx handled the request but found nothing to serve. Check these in order.
- Which server block answered? If the
Hostheader matches noserver_name, Nginx uses the default server for that port, which may have a differentroot. Test withcurl -H 'Host: example.com' http://127.0.0.1/. - Which location block answered? A regex location can take priority over a prefix location you expected to win.
- How is the path built? With
root, the URI is appended to the path. Withalias, the matched part of the URI is replaced. Mixing them up is a classic error.
location /static/ {
alias /var/www/example.com/assets/;
}With alias, a request for /static/app.css maps to /var/www/example.com/assets/app.css. With root in the same place, it would map to /var/www/example.com/assets/static/app.css. Keep the trailing slashes consistent between the location and the alias.
The error log records the full path Nginx tried, which settles the question:
open() "/var/www/example.com/html/missing.css" failed (2: No such file or directory)502 Bad Gateway and 504 Gateway Timeout
Both come from reverse proxying. A 502 means Nginx could not get a valid answer from the backend. A 504 means the backend accepted the connection but did not reply within the timeout.
First, test the backend directly, bypassing Nginx.
curl -i http://127.0.0.1:3000/
sudo ss -tlnp | grep 3000If that fails, the problem is the application, not Nginx. If it works, read the error log:
connect() failed (111: Connection refused) while connecting to upstream
upstream timed out (110: Connection timed out) while reading response header from upstream
upstream prematurely closed connection while reading response header from upstreamConnection refused: the application is not running, listens on a different port, or listens only on a different address. Check theproxy_passaddress against the real listener.timed out: the application is slow. Fix the application first. Raise the timeout only if long requests are legitimate.prematurely closed: the application crashed or reset the connection. Read the application's own logs.Permission deniedconnecting to upstream on SELinux: allow it withsudo setsebool -P httpd_can_network_connect 1.
A frequent trap is localhost in proxy_pass. It can resolve to the IPv6 address ::1 while the application listens only on IPv4. Using 127.0.0.1 explicitly avoids this.
If you do need longer timeouts for a specific location, set them there and not globally:
location /reports/ {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 5s;
proxy_read_timeout 120s;
}Redirect loops and wrong schemes
An application behind Nginx sees only plain HTTP from the proxy. If it enforces HTTPS, it may redirect every request again, forever. The fix is to pass the original scheme, as in the proxy example earlier, and to make the application trust the X-Forwarded-Proto header.
curl -sIL --max-redirs 5 http://example.com/ | grep -Ei '^(HTTP|location)'This follows at most five redirects and prints only the status lines and targets, so a loop is easy to see.
Reading the logs efficiently
The access log answers "what did clients ask for, and what did we reply." The error log answers "why did that fail." Use both together by matching timestamps.
sudo tail -f /var/log/nginx/error.log
sudo awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
sudo grep ' 502 ' /var/log/nginx/access.log | tailThe second command counts requests per status code, assuming the default combined format where the status is field 9. If the error log is silent, raise its detail temporarily with error_log /var/log/nginx/error.log debug;. Debug logging requires a binary built with debug support and produces a lot of output, so revert it afterward.
Other common faults
- Changes have no effect. You edited a file that is not included, or you edited but did not reload. Confirm with
nginx -T | grep -n example.com. Browsers also cache redirects and static files, so test withcurl. - Symlink missing in
sites-enabled. On Debian and Ubuntu, a file insites-availableis inactive until linked:sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. - 413 Request Entity Too Large. An upload exceeds
client_max_body_size, which defaults to 1 MB. Raise it in the relevant server or location. - Too many open files. Under heavy load, raise
worker_rlimit_nofileand make sureworker_connectionsfits within it. - Firewall blocks. The service works locally but not remotely. Check
sudo ufw statusorsudo firewall-cmd --list-allfor ports 80 and 443.
Exercise
A site returns 502 Bad Gateway. curl -i http://127.0.0.1:3000/ on the same server returns 200, but the Nginx error log shows connect() failed (111: Connection refused) while connecting to upstream with the upstream shown as [::1]:3000. What is the likely cause and the fix?
Show answer
The configuration uses proxy_pass http://localhost:3000;, and localhost resolved to the IPv6 address ::1. The application listens only on IPv4, so the connection is refused. Change the directive to proxy_pass http://127.0.0.1:3000;, run sudo nginx -t, then reload Nginx.
Exercise
Nginx returns 403 for /var/www/site/html/index.html. The file has mode 644 and is owned by the worker user, yet the error log reports Permission denied. Which command shows the likely cause, and what does it reveal?
Show answer
Run namei -l /var/www/site/html/index.html. It lists the owner and mode of every directory in the path. The likely cause is a parent directory, such as /var/www/site, that lacks the execute bit for the worker user, so Nginx cannot traverse it even though the file itself is readable.