Varnish 前面的 Nginx 有时会产生“从上游读取响应头时上游过早关闭连接”错误

Ole*_*kha 5 nginx varnish

我在同一台服务器上的 Varnish(4.1.0) 前面有 Nginx(1.9.9)。

//nginx
upstream varnish {
    server 127.0.0.1:8391;
    keepalive 16;
}

location ~ \.php$ {
     proxy_pass http://varnish;
     proxy_http_version 1.1; #for 1.0 varnish shows blank page
     proxy_set_header Connection "";
     proxy_redirect off;

     proxy_set_header Host $host:$server_port;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Proto $scheme;

     proxy_read_timeout 600;
     proxy_send_timeout 600;
     proxy_connect_timeout 600;
}

//varnish
DAEMON_OPTS="-a :8391 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,1024m"
Run Code Online (Sandbox Code Playgroud)

对于 0.001% 的请求,nginx 显示错误:

[error] 5331#5331: *7392847 upstream prematurely closed connection while reading response header from upstream, client: xx.xx.xx.xx, server: _, request: "GET /home HTTP/1.1", upstream: "http://127.0.0.1:8391/index.php?q=/home", host: "xxx", referrer: "xxx"
Run Code Online (Sandbox Code Playgroud)

使用 proxy_buffers 没有帮助。

小智 0

我们遇到了同样的问题,使用 Nginx 进行 SSL 和 HTTP2,使用 Varnish 进行缓存,使用 Apache 进行实际的 Web 服务。

\n\n

我注意到它只发生在我们设置绕过 Varnish 的请求上。由于某种原因,多年前,我们在 VCL 文件中使用了 return(pipe) 来实现这些。我将其从 return(pipe) 更改为 return(pass) ,瞧\xc3\xa0,问题解决了。

\n\n

显然,您需要查看 VCL 并确保 return(pass) 适用于您的场景,但在大多数情况下,它可能就是答案。

\n