How to increase timeout between Varnish 4 and Nginx

skonsoft picture skonsoft · Dec 31, 2015 · Viewed 7.7k times · Source

Before asking this question, I searched Google and everywhere for hours trying to increase the connection timeout between Varnish 4 and Nginx but found no solution.

So here is my Varnish Config:

backend web1 {
    .host = "192.168.1.21";
    .port = "80";
    .probe = {
        .request =
        "HEAD / HTTP/1.1"
        "Host: localhost"
        "Connection: close";

        .timeout = 1s;
        .interval = 5s;
        .window = 5;
        .threshold = 3;
    }
    .connect_timeout = 300s;
    .between_bytes_timeout = 300s;
}

And this is Nginx config:

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 0; # wait for 5 minutes before timeout the request

#       proxy_connect_timeout   60s;
#       proxy_send_timeout      300s;
#       proxy_read_timeout      300s;

        fastcgi_read_timeout    300s;

        types_hash_max_size 2048;
        # server_tokens off;

        client_body_buffer_size 20m; #Buffer of post request
        client_header_buffer_size 2k; #Buffer of headers
        client_max_body_size 40m; #Max post request size
        large_client_header_buffers 20 2k; #Max number and size of buffers of large requests

        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+rss text/javascript;
        ##
        # 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/*;
}

I tried all parameters but nothing works.

When I set keepalive_timeout to 300s, if I access it directly, the timeout is set to 300s (5 minutes) but when I access through Varnish, timeout is set to 60s. So I disabled the Nginx keepalive_timeout and setting it to 0, but got the same problem.

What's wrong?

How can I increase timeout between Varnish and Nginx?

Answer

istepaniuk picture istepaniuk · Aug 29, 2018

As answers to this question point out: https://serverfault.com/questions/573169/varnish-503-error-after-exactly-60-seconds-how-to-change-this-timeout-value

The problem seems to be the .first_byte_timeout parameter in the VCL, by default it is only 60 seconds.

Set the first byte timeout to 300s:

.first_byte_timeout = 300s;