How to Fix Nginx Timeout Error on DigitalOcean Droplet
The Root Cause Nginx Timeout Errors on DigitalOcean Droplets frequently stem from the backend application taking longer to process a request than Nginx is configured to wait. This often happens due to insufficient Droplet resources for the application’s load or the application itself being inefficient, leading to Nginx cutting off the connection prematurely.
Quick Fix (CLI) Restart Nginx and the associated backend service to clear any transient issues or hung processes.
sudo systemctl status nginx
sudo systemctl restart nginx
# If your application uses PHP-FPM, restart it (adjust version as necessary):
sudo systemctl restart php8.1-fpm
# If using a different backend (e.g., Gunicorn for Python, PM2 for Node.js), restart that service:
# sudo systemctl restart gunicorn
# pm2 restart all
Configuration Check
Edit the Nginx configuration file responsible for your application. This is typically /etc/nginx/sites-available/your_domain or /etc/nginx/nginx.conf for global settings.
Add or modify the relevant timeout directives within the http, server, or location block. A common initial increase is from 60s to 180s or 300s.
For applications proxied by Nginx (e.g., Node.js, Python Gunicorn):
# Within your 'server' or specific 'location' block
proxy_connect_timeout 300s; # How long Nginx waits to establish a connection with the proxied server
proxy_send_timeout 300s; # How long Nginx waits for the proxied server to send data
proxy_read_timeout 300s; # How long Nginx waits for the proxied server to respond
For PHP applications using PHP-FPM:
# Within your PHP 'location' block
fastcgi_connect_timeout 300s; # How long Nginx waits to connect to PHP-FPM
fastcgi_send_timeout 300s; # How long Nginx waits for PHP-FPM to acknowledge data send
fastcgi_read_timeout 300s; # How long Nginx waits for PHP-FPM to send a response
Verification After modifying the Nginx configuration, test the syntax and reload Nginx. Then, re-access the problematic URL to confirm the fix.
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl status nginx
Access the problematic URL in your browser or via curl to confirm the timeout error is resolved.