'504 Gateway Time-out when using curl on ec2 with Apache and PHP

I have a ec2 instance (not Lightsail) working behind a Load Balancer. Apache, PHP-FPM are installed and WordPress (or any PHP code) is working mostly fine. However if I try to run code using curl I get a 504 Gateway Timeout. WordPress Site Health also fail because of the same issue.

Issues like:

  • Error: cURL error 28: Failed to connect to fakedomainforthispost.com port 443 after 7503 ms: Connection timed out (http_request_failed)
  • Your site is unable to reach WordPress.org at 198.143.164.251, and returned the error: cURL error 28: Connection timed out after 10001 milliseconds
  • The loopback request to your site failed, this means features relying on them are not currently working as expected. Error: cURL error 28: Failed to connect to fakedomainforthispost.com port 443 after 7503 ms: Connection timed out (http_request_failed)

If I run curl as a command in ssh works fine. If I run curl as code inside a php file I get the 504 Gateway Timeout. I tried setting the KeepAlive rules bigger than the ELB Idle time with no success.

If I clone the ec2 instance and run outside the Load Balancer, curl in php works fine.

The load balancer allows ports 80 and 433 and the connected Target group communicates to the instances using port 80. The rest works fine.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source