Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

504 gateway timeout on AWS ec2, without executing long php script

I've encountered http 504 gateway timeout error for several times when I was trying to call a GET API that was programmed by PHP.

Here is my server and AWS environment.

  • An ec2 instance with Amazon Linux that is running php code (5.4.40) with apache server (2.4.12) to serve api calling from client.
  • An AWS elastic load balancer to balance traffic to one of my instances. (for now, I only have one instance, just set ELB for the future if I need more instances to handle traffic.)
  • An AWS RDS database (MySQL 5.6.21) for saving data.

From some articles about 504 gateway timeout, I've already tried to modify these settings:

 # ELB
 idle timeout => 300

 # php.ini
 max_execution_time => 301
 max_input_time => 301

 # httpd conf
 MaxKeepAliveRequests => 100
 KeepAliveTimeout => 30

But all of them are not helpful for me, it's still get 504 gateway timeout sometimes.

My php script is not a long script, it just get data from mysql database (AWS RDS) from 3 tables and return data to client, no uploading file or generateing big file, so I think the execution time is not the problem.

The strange thing is that 504 gateway timeout error is not always happened, most of time it is normal, just happened SOMETIMES, for now, I still don't understand when 504 error will happen, it's really strange, if anyone can give me some suggestions about how to resolve this problem, it's really a big favor for me.

=== New Update ===

I've just found a problem in my php code, I thought that's namespace with autoload problem.

I have 2 php files in the same folder, it means 2 classes with the same namespace

files:

My/Namespace
  - Class1.php
  - Class2.php

Class and namespace:

Class1

// Class1
namespace My\Namespace;
class Class1 {
    public static function getInstance() {
        //return...
    }
}

Class2

// Class2
namespace My\Namespace;
class Class2 {
    public static function getInstance() {
        //return...
    }

    public function getClass1Instance() {
        $class1 = Class1::getInstance();
        return $class1;
    }
}

In Class2.php I try to call Class1's static function, but I didn't add "use namespace", so I add the following line to Class2.php

use My\Namespace\Class1;

Problem was solved! But I still not really sure why I should add "use namespace" to Class2.php, Class1 and Class2 are both in the same namespace, should I add "use namespace" even through they are in the same namespace?

p.s. I found this namespace problem because when 504 gateway error happened, I tried to call the API many times in a short period, and the php error message show up and tell me

"Class1 is not found in Class2.php"

but sometimes php error message show

"Cannot call a overloaded function in Class2.php, getClass1Instance()"

Wish I provide enough message about this question, and thanks for everyone who left comment or answered my question, m(_ _)m

like image 349
Kai Avatar asked Oct 20 '22 11:10

Kai


1 Answers

I suggest you take a look at the Health Check of ELB.

Health Check is a source of seemingly-random 504 errors when it is not properly configured. When the ELB thinks your server is not 'healthy' then ELB answers 504 to the end user, and that 504 error is not logged anywhere in your PHP environment because it was generated in the ELB.

See http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/ts-elb-healthcheck.html

like image 88
Daniel777 Avatar answered Nov 11 '22 08:11

Daniel777