【问题标题】:Varnish / Nginx: initial connection after idle for ~5 secondsVarnish / Nginx:空闲约 5 秒后的初始连接
【发布时间】:2017-08-24 07:22:18
【问题描述】:

我已经在以下位置设置了一个测试服务器:http://95.85.19.39/,在 Ubuntu 14.04 上运行 varnish 4.1.5(端口 80)和 nginx 1.4.6(端口 8080)。我使用的是 Chrome 57.0.2987.110(64 位)。

Varnish 正在缓存页面,但是当您等待 5 秒以上并点击刷新时,您会看到初始连接,这是为什么呢?当您在 5 秒内点击刷新时,不会有初始连接。

如果我删除 varnish 并仅在 Nginx 上运行服务器,那么我将无法再获得初始连接,所以我认为我的 Varnish 有问题。

有谁知道为什么会发生这种情况,我将如何解决这个问题?

无初始连接

初始连接

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"

【问题讨论】:

标签: nginx varnish


【解决方案1】:

你观察到的是正常的。 Varnish 4 中默认的 keepalive 值为 5 秒。

第一个请求涉及额外的 TCP 开销。如果在 5 秒内发生第二次请求,则在现有 TCP 连接上执行,不会进行“初始连接”,节省一些时间。

如果您希望 keepalive 超时超过 5 秒,您可以增加 Varnish 客户端超时。

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

更多关于Keep-Alive in web servers (Varnish, Nginx, Apache)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 2015-08-13
    • 2017-04-15
    • 2021-05-14
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多