大网数据广告活动
服务器运维问答

Varnish cache enabled but still getting age: 0 in header

· 52CE 我爱测

I have installed varnish on my server alongside apache. Its an SSL site. So I use apache for SSL termination with varnish. The problem is when I use the curl -I command, the headers show that age is always more than 0. Same case …

问题原文

原帖链接 · Rahul Biswas · CC BY-SA 4.0

I have installed varnish on my server alongside apache. Its an SSL site. So I use apache for SSL termination with varnish. The problem is when I use the curl -I command, the headers show that age is always more than 0. Same case when I test my site with https://isvarnishworking.uk But, when I see the headers from my browser, it always shows the age as 0. I have tried everything. I even tried it with an http only site. Still in the web browser, the age is shown as 0

the guide that I used: https://bash-prompt.net/guides/apache-varnish/

These are the headers

accept-ranges: bytes
access-control-allow-origin: *
age: 0
content-encoding: gzip
content-length: 7128
content-type: text/html; charset=UTF-8
date: Thu, 15 Aug 2019 05:37:53 GMT
last-modified: Thu, 15 Aug 2019 04:25:12 GMT
server: Apache/2.4.29 (Ubuntu)
status: 200
vary: X-Forwarded-Proto,Accept-Encoding
via: 1.1 varnish (Varnish/5.2)
x-varnish: 65690

EDIT: This is the headers that i get when i use curl -v 127.0.0.1:8080

Rebuilt URL to: 127.0.0.1:8080/
  Trying 127.0.0.1...
TCP_NODELAY set
Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
GET / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: curl/7.58.0
Accept: */*
HTTP/1.1 200 OK
Date: Thu, 15 Aug 2019 09:58:23 GMT
Server: Apache/2.4.29 (Ubuntu)
Link: ; rel="https://api.w.org/", https://arcadesite.io/>; rel=shortlink
Vary: Accept-Encoding
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=UTF-8
X-Varnish: 491654 524537
Age: 62
Via: 1.1 varnish (Varnish/5.2)
Accept-Ranges: bytes
Content-Length: 28467
Connection: keep-alive

已采纳解答

原帖链接 · Danila Vershinin · CC BY-SA 4.0

Why you don't get the same results in browsers and curl is mostly always one - cookies.

Varnish does some precautions for you and bypasses caching in presence of cookies (either when set by the backend, or when you're sending by the browser).

In your case it's most likely that your browser sends a Cookie: in request (can be easily verified in Chrome devtools, Network tab).

To make Varnish do its thing, the typical resolution is configuring it to strip all cookies when no essential cookies are present, e.g. in vcl_recv:

if (req.http.cookie !~ "your-app-cookie-name") {
    unset req.http.cookie;
}

This will take care of only passing through cookies when the your-app-cookie-name is one of the cookies being sent. For other cases (e.g. user not logged, the cookies are likely needed by Javascript tracking scripts, so not essential neither to Varnish nor backend to work properly).

If your app sends "essential" cookie for every page (quote often), you will need to either adjust your app (best route, e.g. send PHPSESSID cookie only in login page) and/or adjust Varnish config to ignore it on specific pages to increase cache hit-ratio.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-08-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。