清漆似乎在工作,但 max-age=0
Varnish appears to be working but max-age=0
isvarnishworking.com 让我知道
Varnish appears to be responding at that url, but the Cache-Control
header's "max-age" value is less than 1, which means that Varnish will
never serve content from cache at this url.
The max-age value appears to be: 0
还有这个header信息
The url we checked: myDomainHere.com
HTTP/1.1 200 OK
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Set-Cookie: PHPSESSID=vgk7db66kh7nce8lpe5789u105; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=60, private, proxy-revalidate
Pragma: no-cache
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 14192
Accept-Ranges: bytes
Date: Sat, 18 Jul 2015 09:31:55 GMT
X-Varnish: 324589322
Age: 0
Via: 1.1 varnish
Connection: keep-alive
我在 .htaccess 中有这个
<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
所以我的问题是,我真的必须更改 max-age=0 才能使清漆性能更好吗?如果是这样,我会在哪里做这个?我在 ubuntu digitalocean 的 droplet
上使用 apache2
-编辑-
这是我的/etc/varnish/default.vcl
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "8080";
}
#
# Below is a commented-out copy of the default VCL logic. If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
# sub vcl_recv {
# if (req.restarts == 0) {
# if (req.http.x-forwarded-for) {
# set req.http.X-Forwarded-For =
# req.http.X-Forwarded-For + ", " + client.ip;
# } else {
# set req.http.X-Forwarded-For = client.ip;
# }
# }
# if (req.request != "GET" &&
# req.request != "HEAD" &&
# req.request != "PUT" &&
# req.request != "POST" &&
# req.request != "TRACE" &&
# req.request != "OPTIONS" &&
# req.request != "DELETE") {
# /* Non-RFC2616 or CONNECT which is weird. */
# return (pipe);
# }
# if (req.request != "GET" && req.request != "HEAD") {
# /* We only deal with GET and HEAD by default */
# return (pass);
# }
# if (req.http.Authorization || req.http.Cookie) {
# /* Not cacheable by default */
# return (pass);
# }
# return (lookup);
# }
#
# sub vcl_pipe {
# # Note that only the first request to the backend will have
# # X-Forwarded-For set. If you use X-Forwarded-For and want to
# # have it set for all requests, make sure to have:
# # set bereq.http.connection = "close";
# # here. It is not set by default as it might break some broken web
# # applications, like IIS with NTLM authentication.
# return (pipe);
# }
#
Do I really have to change that max-age=0 in order to varnish perform
better?
是的。如果你想让它发挥作用,你需要去做。
If so, where would I Do this?
看来您已准备好缓存静态内容,但您似乎还想缓存 PHP 脚本执行响应:
您只是缺少 session_cache_limiter and session_cache_expire,这应该是在请求的 PHP 脚本上执行的第一行,甚至在 session_start()
之前。
例如,如果返回的内容是私有的,并且您想每分钟刷新一次,请尝试以下操作:
session_cache_limiter('private');
session_cache_expire(1);
之后,您应该在 Varnish 响应中看到一个正确的 Cache-Control: max-age=60, private
值,请求 PHP 生成的内容,包括脚本开头的那些行。
[更新] 看到你的问题还没有解决,也许你应该尝试使用 Apache mod_expires 而不是手动设置 Cache-Control header: http://httpd.apache.org/docs/2.4/mod/mod_expires.html
这应该可以回答您的问题,但请允许我给您一些可能对您有用的额外注释,并帮助您更好地理解 Varnish 和 Cache-Control
header 之间的关系彼此:
- 使用默认配置
max-age=0
上的响应指示 Varnish 和
浏览器响应不可缓存。除非另有配置或根据客户的要求明确允许,Varnish will not serve stale content:
A stale cache item will not be returned by any cache (proxy cache or
client cache).
来自 https://www.rfc-editor.org/rfc/rfc7234#section-5.3:
If a response includes a Cache-Control field with the max-age
directive (Section 5.2.2.8), a recipient MUST ignore the Expires
field. Likewise, if a response includes the s-maxage directive
(Section 5.2.2.9), a shared cache recipient MUST ignore the Expires
field. In both these cases, the value in Expires is only intended
for recipients that have not yet implemented the Cache-Control field.
来自 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3:
If a cache returns a stale response, either because of a max-stale
directive on a request, or because the cache is configured to override
the expiration time of a response, the cache MUST attach a Warning
header to the stale response, using Warning 110 (Response is stale).
A cache MAY be configured to return stale responses without
validation, but only if this does not conflict with any "MUST"-level
requirements concerning cache validation (e.g., a "must-revalidate"
cache-control directive).
If both the new request and the cached entry include "max-age"
directives, then the lesser of the two values is used for determining
the freshness of the cached entry for that request.
Varnish 的主要目的是存储 in-memory 可缓存的响应
(当它们是新鲜的时候)所以它们可以被送到相同或不同的地方
无需重新生成客户端,也有助于负载平衡
如果需要。
当请求新鲜内容时,Varnish 将不会提供陈旧内容
(最常见的情况)即使发生了,浏览器也不会
保存它 并且会生成一个新页面的新请求
用户请求该内容的时间,不必要地访问缓存
(涉及所有网络 activity)而不是渲染
本地存储的副本,这将是非常低效且明显的
慢一点!
max-age Indicates that the client is willing to accept a response
whose age is no greater than the specified time in seconds. Unless
max- stale directive is also included, the client is not willing to
accept a stale response.
- 如果还有什么不明白的地方,你可以看看varnish
Cache-Control 上的文档以获取更多详细信息。
现在,如果您仍然看到浏览器在每次加载内容时发送新请求(请检查浏览器开发人员工具的网络选项卡或 varnish logs), recheck everything. As a last resort you can also try to set the proper html meta tags, although html5 deprecates them,它们对于任何现代浏览器都不是必需的这不是最佳做法,所以我建议不要这样做。
这里还有 some good reading 对您可能感兴趣的 PHP 脚本生成的内容进行适当的缓存控制。
您的 Varnish VCL 不够用。根据您的 Varnish 版本,搜索并使用相应的 VCL 3 或 VCL 4 模板。
检查您的 Varnish 版本:varnishd -V
如果您使用版本 2,请升级到版本 3 或 4。
清漆 3 的 VCL:
https://github.com/dreamhost/varnish-vcl-collection
清漆 4 的 VCL:
https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/blob/master/default.vcl
isvarnishworking.com 让我知道
Varnish appears to be responding at that url, but the Cache-Control header's "max-age" value is less than 1, which means that Varnish will never serve content from cache at this url.
The max-age value appears to be: 0
还有这个header信息
The url we checked: myDomainHere.com
HTTP/1.1 200 OK
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Set-Cookie: PHPSESSID=vgk7db66kh7nce8lpe5789u105; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=60, private, proxy-revalidate
Pragma: no-cache
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Type: text/html
Content-Length: 14192
Accept-Ranges: bytes
Date: Sat, 18 Jul 2015 09:31:55 GMT
X-Varnish: 324589322
Age: 0
Via: 1.1 varnish
Connection: keep-alive
我在 .htaccess 中有这个
<FilesMatch "\.(js|css)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=60, private, proxy-revalidate"
</FilesMatch>
所以我的问题是,我真的必须更改 max-age=0 才能使清漆性能更好吗?如果是这样,我会在哪里做这个?我在 ubuntu digitalocean 的 droplet
上使用 apache2-编辑-
这是我的/etc/varnish/default.vcl
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "127.0.0.1";
.port = "8080";
}
#
# Below is a commented-out copy of the default VCL logic. If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
# sub vcl_recv {
# if (req.restarts == 0) {
# if (req.http.x-forwarded-for) {
# set req.http.X-Forwarded-For =
# req.http.X-Forwarded-For + ", " + client.ip;
# } else {
# set req.http.X-Forwarded-For = client.ip;
# }
# }
# if (req.request != "GET" &&
# req.request != "HEAD" &&
# req.request != "PUT" &&
# req.request != "POST" &&
# req.request != "TRACE" &&
# req.request != "OPTIONS" &&
# req.request != "DELETE") {
# /* Non-RFC2616 or CONNECT which is weird. */
# return (pipe);
# }
# if (req.request != "GET" && req.request != "HEAD") {
# /* We only deal with GET and HEAD by default */
# return (pass);
# }
# if (req.http.Authorization || req.http.Cookie) {
# /* Not cacheable by default */
# return (pass);
# }
# return (lookup);
# }
#
# sub vcl_pipe {
# # Note that only the first request to the backend will have
# # X-Forwarded-For set. If you use X-Forwarded-For and want to
# # have it set for all requests, make sure to have:
# # set bereq.http.connection = "close";
# # here. It is not set by default as it might break some broken web
# # applications, like IIS with NTLM authentication.
# return (pipe);
# }
#
Do I really have to change that max-age=0 in order to varnish perform better?
是的。如果你想让它发挥作用,你需要去做。
If so, where would I Do this?
看来您已准备好缓存静态内容,但您似乎还想缓存 PHP 脚本执行响应:
您只是缺少 session_cache_limiter and session_cache_expire,这应该是在请求的 PHP 脚本上执行的第一行,甚至在 session_start()
之前。
例如,如果返回的内容是私有的,并且您想每分钟刷新一次,请尝试以下操作:
session_cache_limiter('private');
session_cache_expire(1);
之后,您应该在 Varnish 响应中看到一个正确的 Cache-Control: max-age=60, private
值,请求 PHP 生成的内容,包括脚本开头的那些行。
[更新] 看到你的问题还没有解决,也许你应该尝试使用 Apache mod_expires 而不是手动设置 Cache-Control header: http://httpd.apache.org/docs/2.4/mod/mod_expires.html
这应该可以回答您的问题,但请允许我给您一些可能对您有用的额外注释,并帮助您更好地理解 Varnish 和 Cache-Control
header 之间的关系彼此:
- 使用默认配置
max-age=0
上的响应指示 Varnish 和 浏览器响应不可缓存。除非另有配置或根据客户的要求明确允许,Varnish will not serve stale content:
A stale cache item will not be returned by any cache (proxy cache or client cache).
来自 https://www.rfc-editor.org/rfc/rfc7234#section-5.3:
If a response includes a Cache-Control field with the max-age
directive (Section 5.2.2.8), a recipient MUST ignore the Expires
field. Likewise, if a response includes the s-maxage directive
(Section 5.2.2.9), a shared cache recipient MUST ignore the Expires
field. In both these cases, the value in Expires is only intended
for recipients that have not yet implemented the Cache-Control field.
来自 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3:
If a cache returns a stale response, either because of a max-stale directive on a request, or because the cache is configured to override the expiration time of a response, the cache MUST attach a Warning header to the stale response, using Warning 110 (Response is stale).
A cache MAY be configured to return stale responses without validation, but only if this does not conflict with any "MUST"-level requirements concerning cache validation (e.g., a "must-revalidate" cache-control directive).
If both the new request and the cached entry include "max-age" directives, then the lesser of the two values is used for determining the freshness of the cached entry for that request.
Varnish 的主要目的是存储 in-memory 可缓存的响应 (当它们是新鲜的时候)所以它们可以被送到相同或不同的地方 无需重新生成客户端,也有助于负载平衡 如果需要。
当请求新鲜内容时,Varnish 将不会提供陈旧内容 (最常见的情况)即使发生了,浏览器也不会 保存它 并且会生成一个新页面的新请求 用户请求该内容的时间,不必要地访问缓存 (涉及所有网络 activity)而不是渲染 本地存储的副本,这将是非常低效且明显的 慢一点!
max-age Indicates that the client is willing to accept a response whose age is no greater than the specified time in seconds. Unless max- stale directive is also included, the client is not willing to accept a stale response.
- 如果还有什么不明白的地方,你可以看看varnish Cache-Control 上的文档以获取更多详细信息。
现在,如果您仍然看到浏览器在每次加载内容时发送新请求(请检查浏览器开发人员工具的网络选项卡或 varnish logs), recheck everything. As a last resort you can also try to set the proper html meta tags, although html5 deprecates them,它们对于任何现代浏览器都不是必需的这不是最佳做法,所以我建议不要这样做。
这里还有 some good reading 对您可能感兴趣的 PHP 脚本生成的内容进行适当的缓存控制。
您的 Varnish VCL 不够用。根据您的 Varnish 版本,搜索并使用相应的 VCL 3 或 VCL 4 模板。
检查您的 Varnish 版本:varnishd -V
如果您使用版本 2,请升级到版本 3 或 4。
清漆 3 的 VCL: https://github.com/dreamhost/varnish-vcl-collection
清漆 4 的 VCL: https://github.com/mattiasgeniar/varnish-4.0-configuration-templates/blob/master/default.vcl