Varnish Age 为 0(通过 nginx 代理到节点应用程序)

Varnish Age 为 0(通过 nginx 代理到节点应用程序)

我正在尝试将 varnish 添加到现有的 Web 堆栈,但似乎无法恢复页面的缓存版本。我想知道我是否做错了。

此配置有效,但我确实不是从 varnish 获得缓存的响应:

 1. Nginx is listening on port 443, request proxied to Varnish
 2. Varnish is listening on port 80, request proxied to Node App
 3. Successful Response

我尝试过的:

curl -kIL https://www.example.com

HTTP/1.1 200 OK
Cache-Control: public, max-age=30
Set-Cookie: locale=en-ca; Path=/
Set-Cookie: locale=en-ca; Path=/
Content-Type: text/html; charset=utf-8
Content-Length: 161861
ETag: W/"27845-EcqEhuo8dduXo4rrAF4EfS6igbQ"
Vary: Accept-Encoding
Date: Wed, 18 Oct 2017 23:57:39 GMT
X-Varnish: 33476
Age: 0
Via: 1.1 varnish-v4
Accept-Ranges: bytes
Connection: keep-alive

对于每个后续请求Age:始终0

VCL 供参考:

vcl 4.0;
import std;

backend example {
    .host = "www.example.com";
    .port = "7600";
}

backend api {
    .host = "api";
    .port = "8080";
}

sub vcl_recv {

    # Remove the port from Host
    set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");

    if (req.http.host ~ "www.example.com") {
        set req.backend_hint = example;
        std.log("hit example.com");
    }


    if (req.http.host ~ "^api") {
        set req.backend_hint = api;
        std.log("hit api");
    }

    if (req.http.User-Agent ~ "Amazon Route 53") {
        return (pass);
    }
}

答案1

您没有缓存内容的原因是Set-Cookie您的应用程序响应中存在标头:

Set-Cookie: locale=en-ca; Path=/
Set-Cookie: locale=en-ca; Path=/

如果您想继续使用 Varnish 并确保它缓存内容,您应该更新您的应用程序以仅在必要时设置 cookie,而不是在每个页面上设置。

对于多语言网站,最好依靠Accept-Language:请求标头并发送Vary语言而不是使用 cookie。

相关内容