我正在尝试使用varnishcaching为login的用户cachingmediawiki页面。 我使用Ubuntu 14.04,清漆4.0.3(端口80)和Nginx作为networking服务器(端口8080)。 作为一个VCL我从mediawiki.org复制configuration,并与我有限的清漆VCL的理解我找不到问题。
现在我遇到了这样的问题:mediawiki页面没有被caching(它们是通过varnish传递的,但是没有被caching)(Age始终是0,只有一个X-Varnish请求id)所以我试图caching一个testingphp文件.php),这是在我的网页的根(即example.com/test.php ),并检查,如果它被正确caching,但它不是。我看到相同的标题,有时caching控制与max-age = 0。test.php文件包含以下内容:
<?php echo 'Hello world';
现在我不知道问题出在哪里,我自己search,但找不到任何线程,至less一个非function性的静态testing页面没有正确caching。 任何人都可以帮我吗?
这里的第一个请求头(我已经取代了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
为了完整,在这里为这个网站的nginxconfiguration:
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
编辑2:和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
编辑3:新的deliverconfiguration后的两个请求的头:
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的caching控制头文件?
第一个请求: 请求头
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
看看你的请求标题。
Cache-Control:no-cache
和
Cache-Control:max-age=0
编辑:这些不是麻烦的原因,可以安全地忽略。 清漆文档陈述:
注意默认情况下,Varnish不关心caching控制请求标头。 如果你想让用户通过强制刷新更新caching,你需要自己做。
顺便说一句。 使用curltesting这些东西是一个好主意,它不会添加任何“随机”标题…
编辑:你的麻烦在于为要求保存的命中 。
- Debug "XXXX HIT-FOR-PASS" - HitPass 2147516455
它保存在先前的请求上,并且告诉varnish不要尝试对相同资源的后续请求进行任何caching。
编辑:好的解决scheme(或者说问题)是在这个VCL的一部分:
if (beresp.ttl < 48h) { set beresp.uncacheable = true; return (deliver); }
很多人说,如果TTL的响应时间短于48小时,那么将响应(或者说请求)设置为不可caching(=通过命中)。 我想不出为什么这是在示例configuration(也许有人会帮助我)的原因。 但是我会试着评论一下,看看会发生什么。
这似乎是一个错误的样本configuration,因为清漆3样本包含这个:
if (beresp.ttl < 48h) { set beresp.ttl = 48h; }
这只是延长到48小时。 (这实际上也很奇怪,但也许它适用于mediawiki)
顺便说一句。 我假设你已经离开你的default_ttl默认值是120秒。 (可以在varnishd命令行上进行更改)
你可以发布/etc/varnish/default.vlcconfiguration文件的内容吗? 看起来你的请求甚至没有去清漆,或者至less去清漆确实内容是否可以从caching到达。
而且,你可以把这个添加到这个configuration文件中,并且在这之后重新启动清漆? 然后从你的客户端( wget或curl )给我们两个连续请求的输出? 特别是对于额外的HTTP X-MISC添加?
sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-MISC = "HIT"; } else { set resp.http.X-MISC = "MIS"; } }