Prevent nginx 504 Gateway timeout using PHP set_time_limit()

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

    You can't use PHP to prevent a timeout issued by nginx.

    To configure nginx to allow more time see the proxy_read_timeout directive.

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

    Using set_time_limit(0) is useless when using php-fpm or similar process manager.

    Bottomline is not to use set_time_limit when using php-fpm, to increase your execution timeout, check this tutorial.

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

    Try this link, it has a better solution on how to fix this. So the steps are:

    1. Open your nginx.conf file located in /etc/nginx directory.
    2. Add this below piece of code under http { section:

      client_header_timeout 3000;
      client_body_timeout 3000;
      fastcgi_read_timeout 3000;
      client_max_body_size 32m;
      fastcgi_buffers 8 128k;
      fastcgi_buffer_size 128k;
      

      Note: If its already present , change the values according.

    3. Reload Nginx and php5-fpm.

      $ service nginx reload
      $ service php5-fpm reload
      

      If the error persists, consider increasing the values.

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

    You need to add extra nginx directive (for ngx_http_proxy_module) in nginx.conf, e.g.:

    proxy_read_timeout 300;
    

    Basically the nginx proxy_read_timeout directive changes the proxy timeout, the FcgidIOTimeout is for scripts that are quiet too long, and FcgidBusyTimeout is for scripts that take too long to execute.

    Also if you're using FastCGI application, increase these options as well:

    FcgidBusyTimeout 300
    FcgidIOTimeout 250
    

    Then reload nginx and PHP5-FPM.

    Plesk

    In Plesk, you can add it in Web Server Settings under Additional nginx directives.

    For FastCGI check in Web Server Settings under Additional directives for HTTP.

    See: How to fix FastCGI timeout issues in Plesk?

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

    Since you're using php-fpm you should take advantage of fastcgi_finish_request() for processing requests you know can take longer.

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

    There are several ways in which you can set the timeout for php-fpm. In /etc/php5/fpm/pool.d/www.conf I added this line:

    request_terminate_timeout = 180
    

    Also, in /etc/nginx/sites-available/default I added the following line to the location block of the server in question:

    fastcgi_read_timeout 180;
    

    The entire location block looks like this:

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 180;
        include fastcgi_params;
    } 
    

    Now just restart php-fpm and nginx and there should be no more timeouts for requests taking less than 180 seconds.

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