How to Fix Common Nginx Errors (502/504/403)

A reference guide to the Nginx errors you're most likely to encounter in production, with the direct cause and fix for each.

502 Bad Gateway

Nginx couldn't get a valid response from the backend/upstream server it's proxying to.

Common causes and fixes:

  • Backend app isn't running — check it directly: curl http://127.0.0.1:3000
  • Backend crashed — check its own logs (e.g. docker logs, or your app's process manager)
  • Wrong port/address in proxy_pass — verify it matches where the app is actually listening
sudo tail -f /var/log/nginx/error.log

504 Gateway Timeout

The backend took too long to respond.

proxy_read_timeout 90s;
proxy_connect_timeout 90s;

Increasing the timeout only masks the symptom if your backend is genuinely too slow — investigate why the app is slow (database query, external API call) as the real fix.

403 Forbidden

Nginx has no permission to serve the requested file, or explicitly denied the request.

sudo chown -R www-data:www-data /var/www/yoursite
sudo chmod -R 755 /var/www/yoursite

Also check for a missing index directive if the error occurs on the site's root:

index index.html index.htm;

404 Not Found (When the File Actually Exists)

Usually a mismatch between root in the config and the file's actual location:

sudo nginx -T | grep root

413 Request Entity Too Large

client_max_body_size 20m;

"nginx: [emerg] ... test failed" on Reload

Always test before reloading to see the exact file/line with the error:

sudo nginx -t

Site Loads but SSL Shows a Warning

Certificate might not cover the exact domain/subdomain requested, or it expired — check with:

sudo certbot certificates

Diagnostic Checklist

  1. sudo nginx -t — is the config even valid?
  2. sudo systemctl status nginx — is the service actually running?
  3. sudo tail -f /var/log/nginx/error.log — the fastest way to find the real cause
  4. For proxy errors, test the backend directly with curl, bypassing Nginx entirely

Where to Look for More Detail

sudo tail -100 /var/log/nginx/error.log
sudo tail -100 /var/log/nginx/access.log

Related Articles

  • How to Install Nginx on Ubuntu & Debian
  • Nginx as a Reverse Proxy for Node.js/Docker Apps
  • Nginx Performance Tuning: Worker Processes, Caching & Gzip
  • nginx errors, 502 bad gateway, 504 gateway timeout, 403 forbidden, nginx troubleshooting
  • 0 Els usuaris han Trobat Això Útil
Ha estat útil la resposta?

Articles Relacionats

How to Install Nginx on Ubuntu & Debian (Complete Guide)

Nginx is one of the world's most widely used web servers, known for its speed, low resource...

How to Install Apache on Ubuntu & Debian (Complete Guide)

Apache HTTP Server is one of the most widely used web servers, valued for its stability, flexible...

Nginx Virtual Hosts (Server Blocks): Hosting Multiple Websites on One VPS

Nginx "server blocks" (the equivalent of Apache's virtual hosts) let you host multiple...

Apache Virtual Hosts: Hosting Multiple Websites on One VPS

Apache Virtual Hosts allow a single server to host multiple independent websites, each identified...

How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps

A reverse proxy sits in front of your application, handling incoming traffic on standard ports...