gpt4 book ai didi

ubuntu - Varnish 似乎在工作,但 max-age=0

转载 作者:太空宇宙 更新时间:2023-11-03 16:43:28 27 4
gpt4 key购买 nike

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

还有这个头信息

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 才能使 Varnish 性能更好吗?如果是这样,我会在哪里做这个?我在 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 performbetter?

是的。如果您想让它发挥作用,您需要去做。

If so, where would I Do this?

看起来您已经准备好缓存静态内容,但您似乎还想缓存 PHP 脚本执行响应:

你只是错过了session_cache_limitersession_cache_expire ,这应该是在请求的 PHP 脚本上执行的第一行,甚至在 session_start() 之前.

例如,如果返回的内容是私有(private)的,并且您想每分钟刷新一次,请尝试以下方法:

session_cache_limiter('private');
session_cache_expire(1);

在那之后你应该看到一个正确的 Cache-Control: max-age=60, private Varnish 对 PHP 生成内容请求的响应值,包括脚本开头的那些行。

[update] 看到您的问题还没有解决,也许您应该尝试使用 Apache mod_expires 而不是手动设置 Cache-Control header :http://httpd.apache.org/docs/2.4/mod/mod_expires.html

这应该可以回答您的问题,但请允许我给您一些可能对您有用的额外说明,并帮助您更好地了解 Varnish 和 Cache-Control 的工作原理。标题相互关联:

  • 使用默认配置max-age=0在响应上指示 Varnish 和浏览器响应不可缓存。除非另有配置或根据客户的要求明确允许,Varnish will not serve stale content :

A stale cache item will not be returned by any cache (proxy cache orclient 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-staledirective on a request, or because the cache is configured to overridethe expiration time of a response, the cache MUST attach a Warningheader to the stale response, using Warning 110 (Response is stale).

A cache MAY be configured to return stale responses withoutvalidation, but only if this does not conflict with any "MUST"-levelrequirements 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 determiningthe freshness of the cached entry for that request.

  • Varnish 的主要目的是在内存中存储可缓存的响应(当它们是新鲜的时候)所以它们可以被送到相同或不同的地方无需重新生成客户端,也有助于负载平衡如果需要的话。

  • Varnish 不会在请求新鲜内容时提供陈旧内容(最常见的情况)即使发生了,the browser would not besaving it并且会生成一个新页面的新请求用户请求该内容的时间,不必要地访问缓存(涉及所有网络事件)而不是渲染本地存储的副本,这将是非常低效且明显的慢一点!

max-age Indicates that the client is willing to accept a responsewhose age is no greater than the specified time in seconds. Unlessmax- stale directive is also included, the client is not willing toaccept a stale response.

现在,如果您仍然看到浏览器在每次加载内容时发送新请求(检查浏览器开发人员工具的网络选项卡或 varnish logs),请重新检查所有内容。作为最后的手段,您也可以 try to set the proper html meta tags , 虽然 html5 deprecates them并且它们对于任何现代浏览器都不是必需的,这不是最佳实践,因此我建议不要这样做。

这里还有some good reading关于对您可能感兴趣的 PHP 脚本生成的内容进行适当的缓存控制。

关于ubuntu - Varnish 似乎在工作,但 max-age=0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31489796/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com