Prevent nginx 504 Gateway timeout using PHP set_time_limit()

后端 未结 10 1893
醉话见心
醉话见心 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: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.

提交回复
热议问题