Nginx – upstream prematurely closed connection while reading response header from upstream 502 Bad GateWay

mac-osxnginxPHP

nginx is killin me.. so right now I have 502 Bad Gateway. error log says:

2016/10/12 17:39:53 [info] 3023#0: *464 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443
2016/10/12 17:39:53 [info] 3023#0: *465 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443
2016/10/12 17:39:55 [error] 3023#0: *459 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: local.beerhawk.co.uk, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "local.mydomain.co.uk"

my ngninx conf file now looks like this:

#user  RobDee;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #log_format  #main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  logs/access.log;
     error_log  logs/error.log;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    server {
        listen       80;
        server_name  default;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/RobDee/workspace/beerhawk/web;
            index  index.html index.htm;
        }

    # HTTPS server


   server {
    listen                     443 ;
    server_name                local.mydomain.co.uk local.beer.telegraph.co.uk;

    ssl                        on;
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate            /usr/local/etc/nginx/cert.pem;
    ssl_certificate_key        /usr/local/etc/nginx/cert.key;

    gzip_disable "msie6";
    gzip_types text/plain application/xml application/x-javascript text/css application/json text/javascript;

    access_log  /usr/local/var/log/nginx/access.log;
    error_log   /usr/local/var/log/nginx/error.log debug;
    log_not_found off;
    root    /Users/RobDee/workspace/beerhawk/web;

    location /.htpasswd
    {
        return 403;
    }
    location ~ \.css {
        root /Users/RobDee/workspace/beerhawk/web;
        expires max;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|js|woff|woff2|ttf)$ {
        root /Users/RobDee/workspace/beerhawk/web;
        access_log off;
        expires max;
    }

    location ~* \.(js|css)$ {
        expires 1y;
        log_not_found off;
    }

    location /
    {
        try_files $uri $uri/ /app_dev.php$is_args$args;
        index index.php app_dev.php;
    }

    location ~ \.php$ {
        #root /Users/RobDee/workspace/beerhawk/web;
    fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  app_dev.php;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        include        fastcgi_params;
        #fastcgi_read_timeout 3000;

    }
    }

    include servers/*;

    }

I have no clue what i'm doing wrong… anybody could help me pls

Best Answer

How long do your applications take to reply?

We see errors similar to these when the client timesout the connection:

2016/10/12 17:39:53 [info] 3023#0: *465 client closed connection while waiting for request, client: 127.0.0.1, server: 0.0.0.0:443

For instance if a varnish instance is timing out at 10s, whilst nginx is happy to wait for 30s for a PHP response, then varnish will terminate the connection before Nginx can respond.

The error upstream prematurely closed connection might be nginx terminating the connection to PHP. Not 100% sure as I can't remember dealing with that error much!

Hope that helps OP :)