How to fix 504 gateway timeout error in aws use ELB
The problem
I have an application PHP + Laravel in AWS with Elastic Beanstalk and Elastic Load Balancer. I have implemented some time-consuming features such as importing CSV files with many records and exporting large datasets. However, the system returns a 504 Gateway Timeout error during these operations. Even though I have increased the timeouts in Nginx and PHP, I still encounter this issue.
How to fix
1. Increase connection idle timeout Elastic Load Balancer
By default, AWS Elastic Load Balancing sets the idle timeout value for your load balancer to 60 seconds. How to change ?
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- On the navigation pane, choose Load Balancers.
- Select the load balancer.
- On the Attributes tab, choose Edit.
- Under Traffic configuration, enter a value for Connection idle timeout. The valid range is 1 through 4000 seconds.
- Choose Save changes.
Reference: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html
2. Increase Nginx Timeout Settings
Nginx Configuration:
http {
...
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
send_timeout 60s;
...
}
Reload the Nginx configuration to apply changes:sudo systemctl reload nginx
3. Increase PHP Timeout Settings
You need to increase the max_execution_time
value in PHP using the following methods:
- Using the
ini_set
function:ini_set('max_execution_time', '300');
- Setting the
max_execution_time
value in thephp.ini
file:
; Maximum execution time of each script, in seconds
; https://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 300