By using AWS re:Post, you agree to the Terms of Use

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 port 443 after 7503 ms: Connection timed out (http_request_failed)
  • Your site is unable to reach at, 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 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.

Kind Regards, D.

1 Answer

Hi, @Deryck

I think your problem is similar to the next article. Please check the countermeasures.

profile picture
answered 5 months ago
  • Thanks but does not look like RHEL is the cause. It was already disabled by default in Amazon Linux. Here's the config file.

    # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted

    Thanks again anyway :)

    Do you have another suggestion?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions