6

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

7
  • How long does it take when it operates normally? Outside of web sockets, long running requests are generally not great for http.
    – datasage
    May 17, 2015 at 20:55
  • If API run normally, it takes 200~1000 ms based on speed of internet, I think it's not a long running request.
    – Kai
    May 18, 2015 at 1:50
  • Are you running php in fcgi mode or mod_php? Do you see anything in your apache error log related to the requests?
    – datasage
    May 18, 2015 at 1:59
  • Im not very sure about fcgi mode and mod_php, I tried to check phpinfo() and see the "Server API" parameter is "Apache 2.0 Handler", does that mean mod_php? And the logs looks normally. I found that my php script query database 18 times in one method, is it the reason?
    – Kai
    May 18, 2015 at 3:33
  • Did you check Apache timeout? See: stackoverflow.com/a/9630627/2838391 ? Also, is there nothing in the elastic beanstalk logs at that time? May 18, 2015 at 12:14

1 Answer 1

3

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

1
  • 1
    I checked the health check of ELB when this error was happening, ELB show me the instance status is InService, so it pass the health check I think, and from my experience, if health check failed, ELB without any instance, the error should be "503 Service Unavailable: Back-end server is at capacity"
    – Kai
    May 18, 2015 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.