1
votes

I've set up a test server on: http://95.85.19.39/ With varnish 4.1.5 (port 80) and nginx 1.4.6 (port 8080) running on Ubuntu 14.04. I'm using Chrome 57.0.2987.110 (64-bit).

Varnish is caching the page, but when you wait 5+ seconds and hit refresh, you'll see an initial connection, why is this? When you hit refresh within 5 seconds, there won't be an initial connection.

If I remove varnish and run the server just on Nginx then I won't get the initial connection anymore, so I assume there's something wrong with my Varnish.

Does anyone know why this is happening and how would I go about fixing this issue?

Without initial connection

Connection Header

With initial connection

Connection Header

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

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

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

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/sites-enabled/default.conf;
}

default.conf

server {
    listen 8080;

    root /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.(htaccess|htpasswd|ini|phps|fla|log|sh)$ {
        deny all;
    }
}

default.vcl

vcl 4.0;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
}

sub vcl_backend_response {
}

sub vcl_deliver {
     if (obj.hits > 0) {
         set resp.http.X-Cache = "HIT";
     } else {
         set resp.http.X-Cache = "MISS";
     }
}

/etc/default/varnish

START=yes
NFILES=131072
MEMLOCK=82000

DAEMON_OPTS="-a :80 \
            -T localhost:6082 \
            -f /etc/varnish/default.vcl \
            -S /etc/varnish/secret \
            -s malloc,256m"
1
@moderator Maybe it's better if this question gets migrated to serverfault.com?Amodar

1 Answers

1
votes

What you observe is normal. The default keepalive value in Varnish 4 is 5 seconds.

First request involves additional TCP overhead. If second request takes place within 5 seconds, it is performed over existing TCP connection and no "initial connection" will be made, saving some time.

You can increase Varnish client timeout if you want keepalive timeout to be more than 5 seconds.

DAEMON_OPTS="-a :80 \
        -T localhost:6082 \
        -f /etc/varnish/default.vcl \
        -S /etc/varnish/secret \
        -s malloc,256m" \
        -p timeout_idle=75 

More on Keep-Alive in web servers (Varnish, Nginx, Apache).