Nginx + PHP-FPM 7.1 - 504 Gateway Time-out

2019-04-21 04:58发布

问题:

I'm running a nginx 1.12 and a php-fpm 7.1 as seperate docker containers on a synology nas and i get a 504 Gateway error if the php-script runs longer than 60s. I've tried already several nginx configuration parameters but the error still exists.

Here is my actual nginx config:

#user  www-data;
#group http
worker_processes  1;

error_log  /opt/data/logs/nginx_error.log notice;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #keepalive_timeout 30s;
    sendfile on;
    #tcp_nopush off;
    tcp_nodelay on;

    #gzip  off;

    send_timeout 300

    server {
        listen       80;
        server_name  "";

        root   /opt/php;
        index  index.php;

        location /data/ {
           sendfile        on;
           root   /opt;
        }

        location ~ \.php$ {

            include fastcgi_params;

            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            # Mitigate https://httpoxy.org/ vulnerabilities
            fastcgi_param HTTP_PROXY "";

            fastcgi_pass   php:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_read_timeout 300;
            #fastcgi_buffering off;
            #fastcgi_keep_conn on;
            #fastcgi_intercept_errors on;
            #fastcgi_cache  off;
            #fastcgi_ignore_client_abort on;

        }

        location ~ ^/(status|ping)$ {
             access_log off;
             include fastcgi_params;
             fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
             fastcgi_pass php:9000;
         }
    }

}

The php-testscript:

<?php 
sleep(65);
echo "done!";
file_put_contents("/opt/data/timetest.txt", "\nEnd", FILE_APPEND);

After 60s the browser shows up the 504 Gateway Time-out. The php-script is still running and is also writing the text to the file.

Nginx errorlog:

2017/07/22 08:16:32 [error] 8#8: *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: , request: "GET /timetest.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "192.168.0.100:8081"

Has anyone an idea?

回答1:

The question is probably why does your backend take so long to respond? Not sure about your usecase but normally it's not user-friendly to wait to long for a response.

To answer your question:
I found this link: https://easyengine.io/tutorials/php/increase-script-execution-time/

Add in /etc/php5/fpm/php.ini

max_execution_time = 300

Set in /etc/php5/fpm/pool.d/www.conf

request_terminate_timeout = 300

Set in /etc/nginx/nginx.conf

http {
 #... 
 fastcgi_read_timeout 300;  
 #... 
}

And in your config:

location ~ \.php$ { 
 include /etc/nginx/fastcgi_params; 
 fastcgi_pass unix:/var/run/php5-fpm.sock; 
 fastcgi_read_timeout 300; 
}

And reload services

service php5-fpm reload 
service nginx reload