Nginx fastcgi_read_timeout Explained: Configuration, 504 Errors, and Best Practices
Introduction
The fastcgi_read_timeout directive specifies how long Nginx waits to receive a response from a FastCGI server, such as PHP-FPM, after a request has been forwarded.
If the FastCGI server does not send any data within the configured timeout period, Nginx terminates the connection and returns a 504 Gateway Timeout error to the client.
Understanding this directive is essential for PHP applications that execute long-running tasks, such as generating reports, processing large file uploads, importing data, or handling complex database queries.
This guide explains how fastcgi_read_timeout works, how to configure it, recommended values, and common configuration mistakes.
Test Environment
What Is fastcgi_read_timeout?
The fastcgi_read_timeout directive determines how long Nginx waits for data from PHP-FPM.
Example:
In this example, Nginx waits up to 60 seconds for PHP-FPM to send data.
If no response is received within that time, Nginx closes the connection.
How It Works
The timeout applies while Nginx is waiting for PHP-FPM to produce output.
Where Is fastcgi_read_timeout Configured?
It is typically configured inside a location block.
Example:
It can also be configured in the http or server context, depending on your requirements.
Check the Current Configuration
Search the configuration files:
grep -R fastcgi_read_timeout /etc/nginx
Example output:
fastcgi_read_timeout 60s;
Change fastcgi_read_timeout
Edit the configuration.
sudo nano /etc/nginx/conf.d/default.conf
Example:
Save the file.
Test the Configuration
Always validate the configuration before reloading.
sudo nginx -t
Expected output:
nginx: the configuration file syntax is ok
nginx: configuration file test is successful
Reload Nginx
Apply the changes.
sudo systemctl reload nginx
Verify:
sudo systemctl status nginx
Expected output:
Active: active (running)
Relationship with PHP
Increasing fastcgi_read_timeout alone does not allow PHP scripts to run longer.
PHP also has its own execution limit.
Common related settings include:
All of these limits should be configured consistently.
Recommended Values
For most production PHP websites:
fastcgi_read_timeout 60s;
is a good starting point.
Common Issues
504 Gateway Timeout
The most common symptom is:
504 Gateway Timeout
Possible causes include:
Slow PHP scripts.
Long-running database queries.
External API requests.
PHP-FPM overload.
Timeout value too low.
Increasing the Timeout Does Not Solve the Problem
If a script normally takes five minutes to finish, simply increasing the timeout may only hide the real issue.
Investigate:
Inefficient PHP code.
Missing database indexes.
Slow SQL queries.
Network latency.
External service delays.
Optimizing the application is usually more effective than using very large timeout values.
PHP Stops Before Nginx
Suppose:
fastcgi_read_timeout 300s;
but PHP is configured as:
max_execution_time = 30
The PHP script still stops after 30 seconds because PHP reaches its own execution limit first.
Best Practices
Use a timeout appropriate for your application's workload.
Optimize slow PHP scripts instead of relying on very large timeout values.
Keep fastcgi_read_timeout, max_execution_time, and PHP-FPM settings aligned.
Monitor PHP-FPM logs when investigating timeout issues.
Test configuration changes before deploying them to production.
Related Articles
PHP max_execution_time Explained
PHP default_socket_timeout Explained
PHP-FPM pm.max_children Explained
Nginx worker_processes Explained
Nginx worker_connections Explained
Nginx keepalive_timeout Explained
Conclusion
The fastcgi_read_timeout directive controls how long Nginx waits for a response from PHP-FPM before returning a 504 Gateway Timeout error. Proper configuration helps ensure that legitimate long-running requests complete successfully while preventing clients from waiting indefinitely.
For most production environments, a timeout of 60 to 120 seconds provides a good balance between reliability and responsiveness. If timeout errors occur frequently, investigate the underlying PHP application and database performance before simply increasing the timeout value.
Explore More
› Nginx proxy_read_timeout Explained: Configuration, 504 Gateway Timeout, and Best Practices
› Nginx Explained: Essential Configuration Directives and Best Practices
› Nginx worker_processes Explained: Configuration, Performance, and Best Practices
› Nginx worker_connections Explained: Configuration, Limits, and Best Practices
› Best Areas to Live in Bangkok in 2026: A Complete Guide for Expats and Digital Nomads
› Best Coworking Spaces in Bangkok 2026: A Guide for Digital Nomads and Remote Workers
› Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained
› Best eSIM for Southeast Asia in 2026: Compare the Top Travel eSIMs