Common Nginx Errors and How to Fix Them (400 to 504)

Key Takeaways
- ✓Two commands find almost every Nginx problem: nginx -t and the error log.
- ✓A table of HTTP errors 400 to 504 with the cause and fix for each.
- ✓Startup errors: port 80 in use, unknown directive, nginx.service not found.
- ✓How to replace the default Nginx error page with your own.
Table of Contents
- •Common Nginx Errors and How to Fix Them (400 to 504)
- •Two Commands That Find Most Problems
- •Nginx HTTP Errors at a Glance
- •Nginx Startup and Config Errors
- •bind() to 0.0.0.0:80 failed (98: Address already in use)
- •unknown directive "..."
- •could not build server_names_hash
- •Failed to restart nginx.service: Unit nginx.service not found
- •Reloading without downtime
- •How to Set a Custom Nginx Error Page
- •Frequently Asked Questions
- •Rather Not Debug Nginx at 2 AM?
Common Nginx Errors and How to Fix Them (400 to 504)
Nginx errors fall into two groups: HTTP status codes that visitors see (403, 404, 502 and so on), and startup or config errors that stop Nginx from starting or reloading. This page covers both and links to a full guide for the most common ones.
For errors that are not specific to Nginx, see our general guides: 502 Bad Gateway, 403 Forbidden, 404 Not Found, ERR_CONNECTION_REFUSED and DNS_PROBE_FINISHED_NXDOMAIN.
Two Commands That Find Most Problems
# Check the configuration for syntax errors
sudo nginx -t
# Watch errors live while you reload the page
sudo tail -f /var/log/nginx/error.logAccess logs are in /var/log/nginx/access.log. If a site defines its own error_log, look there instead. On Docker, Nginx writes to the container output, so use docker logs.
Nginx HTTP Errors at a Glance
| Code | Meaning | Most common cause | Fix |
|---|---|---|---|
| 400 Bad Request | Malformed request | Cookie or header too large; plain HTTP sent to the HTTPS port | Clear cookies; raise large_client_header_buffers 4 16k; |
| 403 Forbidden | Not allowed to serve | Missing index file, permissions, deny rule, SELinux | Full 403 guide |
| 404 Not Found | Path not mapped | root vs alias, missing try_files, wrong server block | Full 404 guide |
| 413 Request Entity Too Large | Upload too big | Default client_max_body_size is 1 MB | Set client_max_body_size 64M; (and PHP upload_max_filesize) |
| 499 Client Closed Request | Visitor gave up | Slow backend; client timed out first | Speed up the upstream |
| 500 Internal Server Error | Error while handling | App crash, or a rewrite loop in Nginx | Check app logs; look for "rewrite or internal redirection cycle" |
| 502 Bad Gateway | Upstream failed | PHP-FPM/Node/container down or wrong address | Full 502 guide |
| 503 Service Unavailable | Temporarily refusing | limit_req/limit_conn limits or maintenance mode | Review rate limits; disable maintenance |
| 504 Gateway Timeout | Upstream too slow | Long queries or scripts | Fix the slow code; raise proxy_read_timeout / fastcgi_read_timeout |
Nginx Startup and Config Errors
bind() to 0.0.0.0:80 failed (98: Address already in use)
Another program (often Apache, or a second Nginx) holds the port. Find it and stop it:
sudo ss -tlpn | grep -E ":80 |:443 "
sudo systemctl disable --now apache2unknown directive "..."
A typo, a missing semicolon on the line above, or a directive from a module that is not installed (for example brotli or more_set_headers). nginx -t prints the file and line number.
could not build server_names_hash
Too many or very long domain names. Add server_names_hash_bucket_size 128; inside the http block.
Failed to restart nginx.service: Unit nginx.service not found
systemd does not know an Nginx service on this machine. Either Nginx is not installed (sudo apt install nginx or sudo dnf install nginx), it was installed another way (a Docker container, a control panel, or OpenResty with its own service name), or you are inside a container where systemd is not running. Check with systemctl list-unit-files | grep -i nginx and which nginx.
Reloading without downtime
Always test first, then reload rather than restart; a reload keeps serving traffic while the new config loads:
sudo nginx -t && sudo systemctl reload nginxHow to Set a Custom Nginx Error Page
Replace the plain default page with your own branded one:
server {
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /404.html { root /var/www/errors; internal; }
location = /50x.html { root /var/www/errors; internal; }
}internal stops visitors from opening the error page directly. For errors returned by a proxied app, add proxy_intercept_errors on; so Nginx shows your page instead of the app's.
Frequently Asked Questions
Where is the Nginx error log?
Usually /var/log/nginx/error.log. Run sudo nginx -T | grep error_log to see every log path in your config.
How do I check if my Nginx config is valid?
Run sudo nginx -t. It reports "syntax is ok" and "test is successful", or the exact file and line of the error.
What is the difference between nginx reload and restart?
Reload applies new config gracefully without dropping connections. Restart stops and starts the process, briefly interrupting traffic.
How do I hide the Nginx version on error pages?
Add server_tokens off; to the http block. The page will say only nginx.
Rather Not Debug Nginx at 2 AM?
Every error in this guide is something a managed host fixes for you. RackUp IT managed hosting runs a tuned Nginx, PHP and caching stack with monitoring, backups and SSL handled, so you can spend your time on the site instead of the server.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
VPS HostingVultr Alternatives in 2026: VPS Hosting for Developers Compared
Vultr's Regular Performance prices compared with DigitalOcean, Linode, Hetzner and a fixed-plan VPS at the 8 GB size, plus when container hosting fits better.
VPS HostingWhat Is a VPS? Virtual Private Servers Explained
A VPS (virtual private server) is a virtual machine with its own operating system, resources and root access. How it works, uses, and when you need one.
VPS HostingLinode (Akamai) Alternatives in 2026: VPS Hosting Compared
Linode's shared CPU prices from Akamai's own API, compared with DigitalOcean, Vultr and a fixed-plan VPS, plus when container hosting makes more sense.