Varnish 与 MediaWiki 不缓存

Varnish 与 MediaWiki 不缓存

我正在尝试使用 varnish cache 为已登录用户缓存 mediawiki 页面。我使用 ubuntu 14.04、varnish 4.0.3(端口 80)和 nginx 作为 Web 服务器(端口 8080)。作为 vcl,我从媒体维基百科并且由于我对 varnish 的 vcl 的了解有限,我找不到问题。

现在我遇到的问题是 mediawiki 页面没有被缓存(它们通过 varnish 传递,但未被缓存(Age 始终为 0 且只有一个 X-Varnish 请求 ID)。因此,我尝试缓存一个测试 php 文件(test.php),该文件位于我的网页根目录中(即example.com/test.php),并检查它是否被正确缓存,但事实并非如此。我看到相同的标头,有时还看到 max-age=0 的 Cache-control。test.php 文件包含以下内容:

<?php
echo 'Hello world';

现在我不知道问题出在哪里,我自己搜索了一下,但找不到任何线程,至少没有找到一个不起作用的静态测试页面没有正确缓存。有人能帮我吗?

这里是第一个请求的标头(我已经用示例替换了 IP 和域:)):

请求标头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Host:example.com
Pragma:no-cache
User-Agent:Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.72 Safari/537.36
Remote Address:0.0.0.0:80
Request URL:http://example.com/test.php
Request Method:GET
Status Code:200 OK

响应标头

Accept-Ranges:bytes
Age:0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html
Date:Sun, 10 May 2015 20:05:05 GMT
Server:nginx/1.4.6 (Ubuntu)
Transfer-Encoding:chunked
Via:1.1 varnish-v4
X-Powered-By:HHVM/3.7.0
X-Varnish:2

第二个请求的标头如下:

请求标头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Host:example.com
User-Agent:Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.72 Safari/537.36
Remote Address:0.0.0.0:80
Request URL:http://example.com/test.php
Request Method:GET

响应标头

Status Code:200 OK
Accept-Ranges:bytes
Age:0
Connection:keep-alive
Content-Encoding:gzip
Content-Length:24
Content-Type:text/html
Date:Sun, 10 May 2015 20:07:49 GMT
Server:nginx/1.4.6 (Ubuntu)
Via:1.1 varnish-v4
X-Powered-By:HHVM/3.7.0
X-Varnish:5

为了完整性,这里提供此站点的 nginx 配置:

server {
    # Running port
    listen 8080;
    server_name example.com;

    # Root directory
    root /data/www/example.com/main;
    index index.php;

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
        client_max_body_size 5m;
        client_body_timeout 60;

        location / {
                try_files $uri $uri/ @rewrite;
        }

        location @rewrite {
                rewrite ^/(.*)$ /index.php?title=$1&$args;
        }

        location ^~ /maintenance/ {
                return 403;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                try_files $uri /index.php;
                expires max;
                log_not_found off;
        }

        location = /_.gif {
                expires max;
                empty_gif;
        }

        location ^~ /cache/ {
                deny all;
        }

        location /dumps {
                root /data/www/example.com/local;
                autoindex on;
        }
}

编辑:使用 curl 的响应标头:

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 10 May 2015 20:42:01 GMT
Content-Type: text/html
X-Powered-By: HHVM/3.7.0
Vary: Accept-Encoding
X-Varnish: 17
Age: 0
Via: 1.1 varnish-v4
Content-Length: 4
Connection: keep-alive
Accept-Ranges: bytes

EDIT2:这个请求的 varnishlog 是:

*   << BeReq    >> 53
-   Begin          bereq 52 pass
-   Timestamp      Start: 1431291731.991895 0.000000 0.000000
-   BereqMethod    GET
-   BereqURL       /test.php
-   BereqProtocol  HTTP/1.1
-   BereqHeader    User-Agent: curl/7.35.0
-   BereqHeader    Host: example.com
-   BereqHeader    Accept: */*
-   BereqHeader    X-Forwarded-For: 0.0.0.1
-   BereqHeader    X-Varnish: 53
-   VCL_call       BACKEND_FETCH
-   VCL_return     fetch
-   Backend        17 default default(127.0.0.1,,8080)
-   Timestamp      Bereq: 1431291731.991930 0.000035 0.000035
-   Timestamp      Beresp: 1431291731.992944 0.001049 0.001014
-   BerespProtocol HTTP/1.1
-   BerespStatus   200
-   BerespReason   OK
-   BerespHeader   Server: nginx/1.4.6 (Ubuntu)
-   BerespHeader   Date: Sun, 10 May 2015 21:02:11 GMT
-   BerespHeader   Content-Type: text/html
-   BerespHeader   Transfer-Encoding: chunked
-   BerespHeader   Connection: keep-alive
-   BerespHeader   X-Powered-By: HHVM/3.7.0
-   BerespHeader   Vary: Accept-Encoding
-   TTL            RFC 120 -1 -1 1431291732 1431291732 1431291731 0 0
-   VCL_call       BACKEND_RESPONSE
-   TTL            VCL -1 120 0 1431291732
-   VCL_return     deliver
-   Storage        malloc Transient
-   ObjProtocol    HTTP/1.1
-   ObjStatus      200
-   ObjReason      OK
-   ObjHeader      Server: nginx/1.4.6 (Ubuntu)
-   ObjHeader      Date: Sun, 10 May 2015 21:02:11 GMT
-   ObjHeader      Content-Type: text/html
-   ObjHeader      X-Powered-By: HHVM/3.7.0
-   ObjHeader      Vary: Accept-Encoding
-   Fetch_Body     2 chunked stream
-   BackendReuse   17 default(127.0.0.1,,8080)
-   Timestamp      BerespBody: 1431291731.993028 0.001132 0.000083
-   Length         4
-   BereqAcct      129 0 129 212 13 225
-   End

*   << Request  >> 52
-   Begin          req 51 rxreq
-   Timestamp      Start: 1431291731.991840 0.000000 0.000000
-   Timestamp      Req: 1431291731.991840 0.000000 0.000000
-   ReqStart       79.193.10.211 52555
-   ReqMethod      GET
-   ReqURL         /test.php
-   ReqProtocol    HTTP/1.1
-   ReqHeader      User-Agent: curl/7.35.0
-   ReqHeader      Host: example.com
-   ReqHeader      Accept: */*
-   ReqHeader      X-Forwarded-For: 0.0.0.1
-   VCL_call       RECV
-   ReqUnset       X-Forwarded-For: 0.0.0.1
-   ReqHeader      X-Forwarded-For: 0.0.0.1
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   Debug          "XXXX HIT-FOR-PASS"
-   HitPass        2147516455
-   VCL_call       PASS
-   VCL_return     fetch
-   Link           bereq 53 pass
-   Timestamp      Fetch: 1431291731.993038 0.001199 0.001199
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx/1.4.6 (Ubuntu)
-   RespHeader     Date: Sun, 10 May 2015 21:02:11 GMT
-   RespHeader     Content-Type: text/html
-   RespHeader     X-Powered-By: HHVM/3.7.0
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     X-Varnish: 52
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   VCL_return     deliver
-   Timestamp      Process: 1431291731.993080 0.001240 0.000041
-   RespHeader     Content-Length: 4
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: keep-alive
-   RespHeader     Accept-Ranges: bytes
-   Timestamp      Resp: 1431291731.993190 0.001350 0.000110
-   Debug          "XXX REF 1"
-   ReqAcct        82 0 82 269 4 273
-   End

*   << Session  >> 51
-   Begin          sess 0 HTTP/1
-   SessOpen       0.0.0.1 52555 :80 0.0.0.1 80 1431291731.989801 14
-   Link           req 52 rxreq
-   SessClose      REM_CLOSE 0.037
-   End

EDIT3:新deliver配置后的两个请求的标头:

florian@florian-VirtualBox:/var/www/html/w$ curl -v http://example.com/test.php
* Hostname was NOT found in DNS cache
*   Trying 0.0.0.0...
* Connected to example.com (0.0.0.0) port 80 (#0)
> GET /test.php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: example.com
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Sun, 10 May 2015 21:32:52 GMT
< Content-Type: text/html
< X-Powered-By: HHVM/3.7.0
< X-Varnish: 2
< Age: 0
< Via: 1.1 varnish-v4
< X-MISC: MIS
< Transfer-Encoding: chunked
< Connection: keep-alive
< Accept-Ranges: bytes
florian@florian-VirtualBox:/var/www/html/w$ curl -v http://example.com/test.php
* Hostname was NOT found in DNS cache
*   Trying 0.0.0.0...
* Connected to dexample.com (0.0.0.0) port 80 (#0)
> GET /test.php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: example.com
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Sun, 10 May 2015 21:32:53 GMT
< Content-Type: text/html
< X-Powered-By: HHVM/3.7.0
< Vary: Accept-Encoding
< X-Varnish: 32770
< Age: 0
< Via: 1.1 varnish-v4
< X-MISC: MIS
< Content-Length: 4
< Connection: keep-alive
< Accept-Ranges: bytes

EDITX:现在正在处理第一个请求,但所有后续请求都由后端传递,这里是标题:/听起来是假的,Varnish 发送了一个 max-age=0 缓存控制标题?

第一个请求: 请求标头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:example.com
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36

响应标头

Accept-Ranges:bytes
Age:32
Cache-Control:s-maxage=18000, must-revalidate, max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-language:de
Content-Length:12102
Content-Type:text/html; charset=UTF-8
Date:Sun, 10 May 2015 22:33:34 GMT
Last-Modified:Sun, 10 May 2015 17:33:34 GMT
Server:nginx/1.4.6 (Ubuntu)
Vary:Accept-Encoding, Cookie
Via:1.1 varnish-v4
X-Content-Type-Options:nosniff
X-Powered-By:HHVM/3.7.0
X-UA-Compatible:IE=Edge
X-Varnish:131094 32817

第二个请求: 请求标头

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:centralnotice_bucket=0-4.2; __atuvc=1%7C19; __atuvs=554fdce151aee950000; mediaWiki.user.bucket%3Aext.articleFeedback-tracking=0%3Aignore; clicktracking-session=gCQO3Eyuh6ZzPCP0q9mTliq4RNSPD1u2O
Host:example.com
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36

响应标头

Accept-Ranges:bytes
Age:0
Cache-Control:s-maxage=18000, must-revalidate, max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-language:de
Content-Type:text/html; charset=UTF-8
Date:Sun, 10 May 2015 22:37:48 GMT
Last-Modified:Sun, 10 May 2015 17:37:48 GMT
Server:nginx/1.4.6 (Ubuntu)
Transfer-Encoding:chunked
Vary:Accept-Encoding, Cookie
Via:1.1 varnish-v4
X-Content-Type-Options:nosniff
X-Powered-By:HHVM/3.7.0
X-UA-Compatible:IE=Edge
X-Varnish:65630

答案1

查看您的请求标头。

Cache-Control:no-cache

Cache-Control:max-age=0

编辑:这些并不是麻烦的根源,可以放心地忽略。Varnish 文档陈述:

注意默认情况下,Varnish 不关心 Cache-Control 请求标头。如果您想让用户通过强制刷新来更新缓存,则需要自己执行此操作。

顺便说一句,使用 curl 测试这些东西是一个好主意,这样不会添加任何“随机”标题......

编辑:你的问题在于击球传球正在为请求保存。

-   Debug          "XXXX HIT-FOR-PASS"
-   HitPass        2147516455

它保存在前一个请求中,并告诉 varnish 不要在对同一资源的后续请求中尝试任何缓存。

编辑:好的,解决方案(或者更确切地说是问题)在 VCL 的这一部分:

    if (beresp.ttl < 48h) {
      set beresp.uncacheable = true;
      return (deliver);
    }

这基本上意味着,如果响应的 TTL 短于 48 小时,则将该响应(或请求)设置为不可缓存(=命中为通过)。我想不出为什么在示例配置中会出现这种情况(也许有人会帮助我)。但我会尝试将其注释掉,看看会发生什么。

这似乎是示例配置的一个错误,因为 varnish 3 示例包含以下内容:

    if (beresp.ttl < 48h) {
      set beresp.ttl = 48h;
    }

这只是将 ttl 延长至 48 小时。(这实际上也很奇怪,但也许它适用于 mediawiki)

顺便说一句。我假设您保留default_ttl默认值,即 120 秒。(可以在varnishd命令行上更改)

答案2

你能发布配置文件的内容/etc/varnish/default.vlc吗?看起来你的请求甚至没有到达 varnish,或者至少经过它并且 varnish 会检查内容是否可以从缓存中访问。

此外,您能将其添加到此配置文件中,然后重新启动 varnish 吗?然后从您的客户端(wgetcurl)给我们两个连续请求的输出?特别是对于添加的额外 HTTP X-MISC?

sub vcl_deliver {
    if (obj.hits > 0) {
            set resp.http.X-MISC = "HIT";
    } else {
            set resp.http.X-MISC = "MIS";
    }
}

相关内容