Prevent nginx 504 Gateway timeout using PHP set_time_limit()

后端 未结 10 1822
醉话见心
醉话见心 2020-11-29 15:58

I am getting 504 timeouts message from nginx when my PHP script is running longer than usual. set_time_limit(0) does not seem to prevent that! Does it not work

相关标签:
10条回答
  • 2020-11-29 16:52

    The correct answer is increasing fastcgi_read_timeout in your Nginx configuration.
    Simple as that!

    0 讨论(0)
  • 2020-11-29 16:55

    I solve this trouble with config APACHE ! All methods (in this topic) is incorrect for me... Then I try chanche apache config:

    Timeout 3600

    Then my script worked!

    0 讨论(0)
  • 2020-11-29 16:59
     sudo nano /etc/nginx/nginx.conf
    

    Add these variables to nginx.conf file:

    http {  
      # .....
      proxy_connect_timeout       600;
      proxy_send_timeout          600;
      proxy_read_timeout          600;
      send_timeout                600;
    }
    

    And then restart:

    service nginx reload
    
    0 讨论(0)
  • 2020-11-29 16:59

    There are three kinds of timeouts which can occur in such a case. It can be seen that each answer is focused on only one aspect of these possibilities. So, I thought to write it down so someone visiting here in future does not need to randomly check each answer and get success without knowing which worked.

    1. Timeout the request from requester - Need to set timeout header ( see the header configuration in requesting library)
    2. Timeout from nginx while making the request ( before forwarding to the proxied server) eg: Huge file being uploaded
    3. Timeout after forwarding to the proxied server, server does not reply back nginx in time. eg: Time consuming scripts running at server

    So the fixes for each issue are as follows.

    1. set timeout header eg: in ajax

    $.ajax({
        url: "test.html",
        error: function(){
            // will fire when timeout is reached
        },
        success: function(){
            //do something
        },
        timeout: 3000 // sets timeout to 3 seconds
    });

    1. nginx Client timeout

      http{
           #in seconds
          fastcgi_read_timeout 600;
          client_header_timeout 600;
          client_body_timeout 600;
       }
      
    2. nginx proxied server timeout

      http{
        #Time to wait for the replying server
         proxy_read_timeout 600s;
      
      }
      

    So use the one that you need. Maybe in some cases, you need all these configurations. I needed.

    0 讨论(0)
提交回复
热议问题