Varnish 不会为某些 wordpress 网站缓存 .js/.css/.HTML

Varnish 不会为某些 wordpress 网站缓存 .js/.css/.HTML

我在 varnish 前面使用 Nginx 作为 SSL 终止器。后端是 litespeed。我有两个 wordpress 网站,它们都使用 HTTPS 协议。我在基础设施前面有一个 Haproxy,它只将 SSL 流量传递给 Nginx。

我的一个网站加载速度很快,当我通过firebug检查缓存项目的状态时,所有内容都得到了“HIT”,如.js .css和域名:domain1.com。

当我为第二个 wordpress 网站检查这些项目时,.js 和 HTML 文件得到了“MISS”,与域名 domain2.com 相同。

两个网站的 Nginx 和 Varnish 以及后端的配置相同。我还禁用了所有插件以及我认为会影响缓存的插件:WP Fastest Cache、404 to 301、Broken Link Checker 和其他一些插件。

唯一的区别是,第二个网站有一个机器人,它可以从其他网站收集新闻并将其发布到网站上(某种网络爬虫)。

什么原因导致清漆不能缓存第二个站点上的所有内容?


编辑:
我为未缓存的请求添加了 varnish 日志:

varnishlog -g request -q "ReqUrl ~ '/wp-content/themes/farda/js/jquery.min.js'"
*   << Request  >> 149       
-   Begin          req 148 rxreq
-   Timestamp      Start: 1499626236.025652 0.000000 0.000000
-   Timestamp      Req: 1499626236.025652 0.000000 0.000000
-   ReqStart       192.168.1.108 57470
-   ReqMethod      GET
-   ReqURL         /wp-content/themes/farda/js/jquery.min.js
-   ReqProtocol    HTTP/1.0
-   ReqHeader      X-Real-IP: 192.168.1.105
-   ReqHeader      X-Forwarded-For: 192.168.1.105
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      X-Nginx: on
-   ReqHeader      Host: mytourguide.ir
-   ReqHeader      Connection: close
-   ReqHeader      User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0
-   ReqHeader      Accept: */*
-   ReqHeader      Accept-Language: en-US,en;q=0.5
-   ReqHeader      Accept-Encoding: gzip, deflate, br
-   ReqHeader      Referer: https://mytourguide.ir/
-   ReqHeader      Pragma: no-cache
-   ReqHeader      Cache-Control: no-cache
-   ReqUnset       X-Forwarded-For: 192.168.1.105
-   ReqHeader      X-Forwarded-For: 192.168.1.105, 192.168.1.108
-   VCL_call       RECV
-   ReqHeader      Cookie: 
-   ReqUnset       Cookie: 
-   ReqHeader      Cookie: 
-   ReqUnset       Cookie: 
-   ReqHeader      Cookie: 
-   ReqUnset       Cookie: 
-   ReqHeader      Cookie: 
-   ReqUnset       Cookie: 
-   ReqUnset       Accept-Encoding: gzip, deflate, br
-   ReqHeader      Accept-Encoding: gzip
-   ReqUnset       X-Forwarded-For: 192.168.1.105, 192.168.1.108
-   ReqHeader      X-Forwarded-For: 192.168.1.108
-   ReqUnset       Accept-Language: en-US,en;q=0.5
-   ReqUnset       User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:51.0) Gecko/20100101 Firefox/51.0
-   ReqHeader      cookie: 
-   ReqUnset       cookie: 
-   ReqHeader      cookie: 
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   Debug          "XXXX MISS"
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 150 fetch
-   Timestamp      Fetch: 1499626236.029726 0.004075 0.004075
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Cache-Control: max-age=2592000, private
-   RespHeader     Expires: Mon, 09 Jul 2018 18:50:18 GMT
-   RespHeader     Last-Modified: Tue, 17 Jan 2017 14:46:21 GMT
-   RespHeader     Content-Type: text/javascript; charset=UTF-8
-   RespHeader     Content-Length: 33342
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Vary: Accept-Encoding,User-Agent
-   RespHeader     Date: Sun, 09 Jul 2017 18:50:18 GMT
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Server: LiteSpeed
-   RespHeader     X-Varnish: 149
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     X-Cache: MISS
-   RespUnset      X-Varnish: 149
-   RespUnset      Via: 1.1 varnish-v4
-   RespHeader     X-Configured-By: ServerSetup.ir
-   VCL_return     deliver
-   Timestamp      Process: 1499626236.029739 0.004087 0.000012
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: close
-   RespHeader     Accept-Ranges: bytes
-   Timestamp      Resp: 1499626236.030691 0.005039 0.000952
-   Debug          "XXX REF 2"
-   ReqAcct        445 0 445 448 33342 33790
-   End            
**  << BeReq    >> 150       
--  Begin          bereq 149 fetch
--  Timestamp      Start: 1499626236.025722 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /wp-content/themes/farda/js/jquery.min.js
--  BereqProtocol  HTTP/1.0
--  BereqHeader    X-Real-IP: 192.168.1.105
--  BereqHeader    X-Forwarded-Proto: https
--  BereqHeader    X-Nginx: on
--  BereqHeader    Host: mytourguide.ir
--  BereqHeader    Accept: */*
--  BereqHeader    Referer: https://mytourguide.ir/
--  BereqHeader    Pragma: no-cache
--  BereqHeader    Accept-Encoding: gzip
--  BereqHeader    X-Forwarded-For: 192.168.1.108
--  BereqHeader    cookie: 
--  BereqProtocol  HTTP/1.1
--  BereqHeader    X-Varnish: 150
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  Backend        17 apache web1(192.168.1.11,,5050)
--  Timestamp      Bereq: 1499626236.025798 0.000076 0.000076
--  Timestamp      Beresp: 1499626236.027971 0.002249 0.002173
--  BerespProtocol HTTP/1.1
--  BerespStatus   200
--  BerespReason   OK
--  BerespHeader   Cache-Control: max-age=2592000, private
--  BerespHeader   Expires: Mon, 09 Jul 2018 18:50:18 GMT
--  BerespHeader   Last-Modified: Tue, 17 Jan 2017 14:46:21 GMT
--  BerespHeader   Content-Type: text/javascript; charset=UTF-8
--  BerespHeader   Content-Length: 33342
--  BerespHeader   Content-Encoding: gzip
--  BerespHeader   Vary: Accept-Encoding,User-Agent
--  BerespHeader   Date: Sun, 09 Jul 2017 18:50:18 GMT
--  BerespHeader   Accept-Ranges: bytes
--  BerespHeader   Server: LiteSpeed
--  BerespHeader   Connection: Keep-Alive
--  TTL            RFC 2592000 -1 -1 1499626236 1499626236 1499626218 1531162218 2592000
--  VCL_call       BACKEND_RESPONSE
--  TTL            VCL 120 10 0 1499626236
--  VCL_return     deliver
--  Storage        malloc Transient
--  ObjProtocol    HTTP/1.1
--  ObjStatus      200
--  ObjReason      OK
--  ObjHeader      Cache-Control: max-age=2592000, private
--  ObjHeader      Expires: Mon, 09 Jul 2018 18:50:18 GMT
--  ObjHeader      Last-Modified: Tue, 17 Jan 2017 14:46:21 GMT
--  ObjHeader      Content-Type: text/javascript; charset=UTF-8
--  ObjHeader      Content-Length: 33342
--  ObjHeader      Content-Encoding: gzip
--  ObjHeader      Vary: Accept-Encoding,User-Agent
--  ObjHeader      Date: Sun, 09 Jul 2017 18:50:18 GMT
--  ObjHeader      Accept-Ranges: bytes
--  ObjHeader      Server: LiteSpeed
--  Fetch_Body     3 length stream
--  Gzip           u F - 33342 95992 80 206366 266672
--  BackendReuse   17 web1(192.168.1.11,,5050)
--  Timestamp      BerespBody: 1499626236.030637 0.004915 0.002666
--  Length         33342
--  BereqAcct      291 0 291 375 33342 33717
--  End            

答案1

问题出在后端(LiteSpeed)。

请注意你的 varnishlog 中的此行:

--  ObjHeader      Cache-Control: max-age=2592000, private

Varnish 内置 VCL 将不缓存发送的 URLCache-Control: private。因此它始终不被缓存,而是MISS稍后再被缓存。

正确的解决方法:配置 LiteSpeed启用公共缓存而不是私有缓存。

相关内容