The Reason of Problem
504 Gateway Timeout error in Amazon EC2 and Elastic Load balancer happens in different scenarios.
Elastic Load Balancer Timeout Scenario
In this case the Timeout value in Apache is greater than Elastic Load Balancer (ELB). Now if the request is taking longer, the ELB will kill it and you will not see anything related to it Apache logs even. You will need to enable ELB access logs to check these errors.
You may need to increase timeout value in ELB. Follow the screenshot below:
EC2 Instance Timeout Scenario
In this case the timeout value in EC2 instance is lesser than ELB. If the request is taking longer than ELB will not kill it. Instead EC2 instance will kill it.
In this scenario we may need to increase the timeout value of Apache in EC2 instance.
To test if the above values in EC2 instance or Elastcic Load Balancer are correct we may need to use below commands:
Testing ELB URL:
root@test:~# time telnet elasticloadbalancerurl.com 443 Trying 127.0.0.1... Connected to elasticloadbalancerurl.com. Escape character is '^]'. Connection closed by foreign host. real 0m51.757s user 0m0.001s sys 0m0.007s root@test:~#
The above result identifies that the timeout value in AWS ELB is 51 seconds. Now we can change the value according to our needs.
Testing EC2 Apache or Nginx:
root@test:~# time telnet 127.0.0.1 80 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. Connection closed by foreign host. real 0m41.504s user 0m0.004s sys 0m0.004s
In case of Apache 2.4, we may need to edit the file: