1

After hours of searching and debugging I give up!

There are thousands of questions and articles about long running PHP processes but non of them solved my issue.

I have a PHP script with the following codes:

$cur = 0; 
    // Second, loop for $timeout seconds checking if process is running 
    while( $cur < 31 ) { 
        sleep(1); 
        $cur += 1; 

       echo "\n ---- $cur ------ \n";
    }

It is simply intended to run for 31 seconds.

I have a Nginx, PHP configured as fastcgi in debian server.

I set max_execution_time = 600

In

/etc/php5/fpm/php.ini

I even set it in

/etc/php5/cli/php.ini

Also set

request_terminate_timeout = 600

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

I also made these changes in nginx.conf http section

client_header_timeout   600;
client_body_timeout     600;
send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers         8 128k;
fastcgi_buffer_size     128k;

And put the directives inside server section. and these directives inside location section of nginx configuration

send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers         8 128k;
fastcgi_buffer_size     128k;

But I still encounter the Gateway Timeout error in the browser! (And Yes! I restarted php-fpm and nginx thousands of times)

Do you guys have any idea?

Here is my nginx.conf

# cat /etc/nginx/nginx.conf 
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

    gzip_min_length 256;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Here is /etc/nginx/conf.d/increase-timeout.conf

client_header_timeout   600;
client_body_timeout     600;
send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers     8 128k;
fastcgi_buffer_size     128k;

And this is my /etc/nginx/sites-enabled/mysite

server {
    listen   IP:80;# default_server;## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /path/to/root;
    index index.html index.php index.htm;

    server_name DOMAIN; 

    access_log /var/log/nginx/mysite-access.log;
    error_log /var/log/nginx/mysite-error.log debug;


    client_header_timeout   600;
    client_body_timeout     600;
    send_timeout           600;

    fastcgi_read_timeout    600;
    fastcgi_send_timeout    600;
    client_max_body_size    20m;

    location / {

        if ($request_uri ~ ^/index.php.*$) {

            rewrite ^(.*)$ $1 last;
        }

        if ($request_filename = "index.html") {

            break;
        }

        if ($request_uri ~ .*php$) {

            set $test A;
        }

        if ($request_filename != "index.php" ) {

                    set $test "${test}B";
            }

        if ($test = "AB") {

            rewrite ^(.*)$ /index.html last;
        }
    }

    location ~ ^/index.php.*$ {

        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;
        fastcgi_keep_conn       on;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;

        send_timeout           600;

        client_max_body_size    20m;

        fastcgi_read_timeout    600;
        fastcgi_send_timeout    600;
        fastcgi_buffer_size             128k;
        fastcgi_buffers                 256 16k;
        fastcgi_busy_buffers_size       256k;
        fastcgi_temp_file_write_size    256k;

                fastcgi_index index.php;
                include fastcgi_params;
        }
}
8
  • Please post the error nginx log entries. Which port is your php-fpm listening on 9000? Aug 12, 2017 at 1:50
  • There is no error! everything is clean. It is unix socket
    – madz
    Aug 12, 2017 at 2:04
  • Please show your nginx and PHP-FPM configuration. Aug 12, 2017 at 8:51
  • The if statements and rewrite statements look really odd here. For example, rewrite ^(.*)$ $1 last; doesn't change the URI at all. What is the purpose of these statements in the first place? Aug 14, 2017 at 17:59
  • Actually I don't remember quite well. I've written these configs years ago and since then had not have any problems with them. I think I meant to ask nginx to use other Location with that rewrite.
    – madz
    Aug 14, 2017 at 18:21

2 Answers 2

1

Try telling nginx to disable output buffering at the scripts runtime with:

header('X-Accel-Buffering: no');

Having said this I just tried your exact example with a fairly stock nginx apache proxy and had no issues other than it waited for the php script to terminate before displaying it.

3
  • Unfortunately this failed either
    – madz
    Aug 12, 2017 at 2:49
  • Sorry to hear that. Any details coming in your php-fpm error_log? It's running long enough you could probably also get it's PID and strace it. Aug 12, 2017 at 2:57
  • Thanks for the reply. php5-fpm.log is clean. Nothing about anything special. There are only reports about the normal restarts and ...
    – madz
    Aug 12, 2017 at 18:00
0

That error is from Nginx, not PHP-FPM. Nginx is complaining that it hasn't received anything in 60 seconds, the default timeout. You can change it:

fastcgi_read_timeout 600s;
1
  • I've already set that. fastcgi_read_timeout 600;
    – madz
    Aug 12, 2017 at 18:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .