cbonavia
By:
cbonavia

Ubuntu 14 Nginx php-fpm - 502 Bad Gateway timeout

July 25, 2016 1.3k views
Nginx PHP Ubuntu

Hi guys, I've a timeout error with Nginx - PHP. After 30 seconds the app return 502 Bad Gateway, always. I've changed fast-cgi params, PHP params, Nginx,etc but the problem still arises.
The process gets images from Amazon S3 services and zip these images in one zip file.

Nginx Conf

user www-data;
worker_processes 1;
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;

        #fastcgi_read_timeout 300;
        #CARLOS Settings
        #client_body_timeout 12;


        #client_max_body_size 1g;

        #client_body_buffer_size 10K;
        #client_header_buffer_size 1k;
        #client_max_body_size 8m;
        #large_client_header_buffers 2 1k;

                #client_body_timeout 300;
        #client_header_timeout 300;
        #keepalive_timeout 300;
        #send_timeout 300;


        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # 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/x-javascript text/xml application/xml application/xml+$

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

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

Site conf.

server {

        listen 80;

        root /var/www/fotopoint.dev/public_html/web;

        server_name dev-admin.fotodelivery.com.ar;

        #rewrite ^/admin\.php/?(.*)$ /$1 permanent;

        location = / {
            index admin.php;
            try_files $uri @symfony;
        }

        location / {
            index admin.php;
            try_files $uri $uri/ @symfony;
        }

        location @symfony {
            rewrite ^(.*)$ /admin.php/$1 last;
        }

        location ~ ^/admin\.php(/|$) {

            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;

            #fastcgi_read_timeout 300; --- doesn't works

            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
}

Nginx - Error log

recv() failed (104: Connection reset by peer) while reading response header
from upstream, client: 186.64.67.120, server: dev-admin.fotodelivery.com.ar, request: "POST /admin/order/zip
HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "dev-admin.fotodelivery.com.ar", referrer:
"http://dev-admin.fotodelivery.com.ar/admin/

PHP -ini parameters changed

upload_max_filesize: 1000M
post_max_size: 1000M

Thanks in advance
Carlos

1 comment
  • Does the FPM service crash 30 seconds after launching it no matter what or does this only happen when you make a request to the application?

    I did notice something strange.

    reading response header
    from upstream, client: 186.64.67.120, server: dev-admin.fotodelivery.com.ar
    

    The error message shown here is resolving dev-admin.fotodelivery.com.ar to a server in Argentina but the domain actually appears to resolve to your droplet (or a DO droplet anyway). I would double-check your upstream settings in

1 Answer

Hi ryanpq. The FPM service crash when I try to zip and download (by PHP) Hundreds of images. All other processes run properly.

I am analysing very closely through tests with another user, and the result indicates, that the problem arise due to the quantity of images not about the timeout.
I perform some test with another pcs (bandwith) and the process takes 45 seconds NOT 30 , so the problem isn't timeout. In all test, the process crash trying to zip image number 110 no matter bandwidth or PC user. Due to this I think the problem can be sockets limit or conection limit, I guess.

Specific PHP process lines:

set_time_limit ( 1200 );    // seting up to 20 minutes
ini_set('max_execution_time', 300);
..
..
   foreach ($orderPhotos as $photo) {
          $finalUrl = $AwsUrl . $photo['photo_name'];
          $zip->addFromString($photo['order_id'] . '/' . $photo['photo_name'], file_get_contents($finalUrl, false, $ctx));
           $imgZipped++;
            $app['session']->set('zipProgress', array('current' => $imgZipped, 'total' => $totalImagesOrders));
                        $app['session']->save();

   }

The function filegetcontent takes the images from AWS bucket and then, these images are added to the zip file.

I look forward to your comments
Thanks
Carlos

Have another answer? Share your knowledge.