How to Fix Nginx Timeout Error on Ubuntu 22.04
-
The Root Cause Nginx timeout errors typically occur when a backend service (e.g., PHP-FPM, Node.js application, or a database query) takes longer to respond than the configured Nginx proxy timeout, or when Nginx takes too long to send a response to the client. On Ubuntu 22.04, like other distributions, the default Nginx configuration values for these timeouts (often 60 seconds) are frequently too conservative for complex web applications or large data transfers, leading to premature connection termination.
-
Quick Fix (CLI) To immediately address common Nginx timeout errors, increase the relevant timeout directives within your main Nginx configuration or specific server block.
First, open the Nginx main configuration file:
sudo nano /etc/nginx/nginx.confLocate the
http { ... }block. If the directives below are not present, add them; if they are, increase their values. A common starting point is 300 seconds (5 minutes).http { # ... other http directives ... # General proxy timeouts for backend applications proxy_connect_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; # Timeout for Nginx to send a response to the client send_timeout 300s; # For PHP-FPM applications, also consider adding/modifying: # fastcgi_connect_timeout 300s; # fastcgi_send_timeout 300s; # fastcgi_read_timeout 300s; # Especially important for long-running PHP scripts }Save the file (Ctrl+O, Enter) and exit nano (Ctrl+X). Then, test the Nginx configuration and reload the service:
sudo nginx -t sudo systemctl reload nginx -
Configuration Check The timeouts should be adjusted based on the specific bottleneck.
- Global Configuration: For settings affecting all virtual hosts, edit
/etc/nginx/nginx.conf, typically within thehttp { ... }block. - Site-Specific Configuration: For timeouts specific to a single website, edit its configuration file in
/etc/nginx/sites-available/your_site_config(e.g.,/etc/nginx/sites-available/default) within theserver { ... }orlocation { ... }blocks.
Key directives to adjust:
proxy_connect_timeout [time]s;: Defines a timeout for establishing a connection with a proxied server.proxy_send_timeout [time]s;: Sets a timeout for transmitting a request to the proxied server.proxy_read_timeout [time]s;: Sets a timeout for reading a response from the proxied server. This is very common for slow backend applications.send_timeout [time]s;: Sets a timeout for transmitting a response to the client. This is relevant if Nginx is slow sending data to the browser.fastcgi_connect_timeout [time]s;,fastcgi_send_timeout [time]s;,fastcgi_read_timeout [time]s;: These are analogous directives specifically for FastCGI (PHP-FPM) connections.fastcgi_read_timeoutis critical for long-running PHP scripts.uwsgi_connect_timeout [time]s;,uwsgi_send_timeout [time]s;,uwsgi_read_timeout [time]s;: Similar directives for uWSGI applications.
Increase these values incrementally until the timeout issue is resolved, but be mindful of setting excessively high values, which can tie up server resources.
- Global Configuration: For settings affecting all virtual hosts, edit
-
Verification After applying configuration changes, always test the Nginx configuration syntax and reload the service.
sudo nginx -t sudo systemctl reload nginxThen, attempt to reproduce the scenario that previously caused the “Nginx Timeout Error” (e.g., re-access the specific web page, rerun the long-running script, or restart the large file transfer). Monitor Nginx error logs (
sudo tail -f /var/log/nginx/error.log) for any new timeout messages or related errors.