HostOnNet Blog

110: Connection timed out while reading response header from upstream

On Nginx + PHP-FPM server, some times site failed with 500 Internal Error. On checking nginx error log, found following error

2017/04/29 06:23:46 [error] 20262#20262: *1720 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 31.50.142.152, server: www.hostonnet.com, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php5.6-fpm.sock", host: "www.hostonnet.com"

The error says “while reading response”, so it is a read_timeout. Nginx have 2 read timeout’s.

  1. proxy_read_timeout
  2. fastcgi_read_timeout

Both have default value of 60 seconds, can be used inside http, server and location blocks. In out case, the error says upstream is fastcgi

upstream: "fastcgi://unix:/run/php/php5.6-fpm.sock",

So what we need to change is fastcgi_read_timeout.

To be safe, i just increased both timeout by editing nginx configuration file and adding

proxy_read_timeout 180;
fastcgi_read_timeout 180;

Default value for proxy_read_timeout/fastcgi_read_timeout is 60 seconds. So any script take more than 60 second time out with internal server error. Some special cause, you may need to allow more than 60 seconds.

Here is the nginx config for the site after modification

upstream php {
        server unix:/run/php/php5.6-fpm.sock;
}

server {

        listen 137.74.94.184:80;
        server_name www.domain.com;
        root /home/domain.com/public_html/;
        index index.php;
        client_max_body_size 800M;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location / {
                # This is cool because no php is touched for static content.
                # include the "?$args" part so non-default permalinks doesn't break when using query string
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
                proxy_read_timeout 180;
                fastcgi_read_timeout 180;
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include /etc/nginx/fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

server {
    listen 137.74.94.184:80;
    server_name  domain.com;
    return       301 http://www.domain.com$request_uri;
}

This is nginx configuration for wordpress site.

You can add this to /etc/nginx/fastcgi.conf, so it will work for all virtual hosts.


Posted in Linux