title: 504 gateway timeout on AWS ec2, without executing long php script
body: <p>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.</p>
  
  <p>Here is my server and AWS environment.</p>
  
  <ul>
  <li>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.</li>
  <li>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.)</li>
  <li>An AWS RDS database (MySQL 5.6.21) for saving data.</li>
  </ul>
  
  <p>From some articles about 504 gateway timeout, I've already tried to modify these settings:</p>
  
  <pre><code> # ELB
   idle timeout =&gt; 300
  
   # php.ini
   max_execution_time =&gt; 301
   max_input_time =&gt; 301
  
   # httpd conf
   MaxKeepAliveRequests =&gt; 100
   KeepAliveTimeout =&gt; 30
  </code></pre>
  
  <p>But all of them are not helpful for me, it's still get 504 gateway timeout sometimes.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>=== New Update ===</strong></p>
  
  <p>I've just found a problem in my php code, I thought that's namespace with autoload problem.</p>
  
  <p>I have 2 php files in the same folder, it means 2 classes with the same namespace</p>
  
  <p>files:</p>
  
  <pre><code>My/Namespace
    - Class1.php
    - Class2.php
  </code></pre>
  
  <p>Class and namespace:</p>
  
  <p>Class1</p>
  
  <pre><code>// Class1
  namespace My\Namespace;
  class Class1 {
      public static function getInstance() {
          //return...
      }
  }
  </code></pre>
  
  <p>Class2</p>
  
  <pre><code>// Class2
  namespace My\Namespace;
  class Class2 {
      public static function getInstance() {
          //return...
      }
  
      public function getClass1Instance() {
          $class1 = Class1::getInstance();
          return $class1;
      }
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>use My\Namespace\Class1;
  </code></pre>
  
  <p>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>
  
  <p>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 </p>
  
  <blockquote>
    <p>"Class1 is not found in Class2.php"</p>
  </blockquote>
  
  <p>but sometimes php error message show </p>
  
  <blockquote>
    <p>"Cannot call a overloaded function in Class2.php, getClass1Instance()"</p>
  </blockquote>
  
  <p>Wish I provide enough message about this question, and thanks for everyone who left comment or answered my question, m(_ _)m</p>