Prevent nginx 504 Gateway timeout using PHP set_time_limit()

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

提交回复
热议问题