●VCL 中文man page(3)
EXAMPLES(例子) 下面這段程式碼和默認的配置相同,後端伺服器主機名設置為「backend.exampl.com」 backend default { .host = "backend.example.com"; .port = "http"; } sub vcl_recv { 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 req.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); } sub vcl_pass { return (pass); } sub vcl_hash { set req.hash += req.url; if (req.http.host) { set req.hash += req.http.host; } else { set req.hash += server.ip; } return (hash); } sub vcl_hit { if (!obj.cacheable) { return (pass); } return (deliver); } sub vcl_miss { return (fetch); } sub vcl_fetch { if (!beresp.cacheable) { return (pass); } if (beresp.http.Set-Cookie) { return (pass); } return (deliver); } sub vcl_deliver { return (deliver); } sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; synthetic {" <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>"} obj.status " " obj.response {"</title> </head> <body> <h1>Error "} obj.status " " obj.response {"</h1> <p>"} obj.response {"</p> <h3>Guru Meditation:</h3> <p>XID: "} req.xid {"</p> <hr> Varnish cache server </body> </html> "}; return (deliver); } 下面的例子顯示一個varnishd實例支援多個獨立的站點,基於請求的URL選擇使用的後端伺服器: backend www { .host = "www.example.com"; .port = "80"; } backend p_w_picpaths { .host = "p_w_picpaths.example.com"; .port = "80"; } sub vcl_recv { if (req.http.host ~ "^(www.)?example.com$") { set req.http.host = "www.example.com"; set req.backend = www; } elsif (req.http.host ~ "^p_w_picpaths.example.com$") { set req.backend = p_w_picpaths; } else { error 404 "Unknown virtual host"; } } The following snippet demonstrates how to force a minimum TTL for all documents. Note that this is not the same as setting the default_ttl run-time parameter, as that only affects document for which the backend did not specify a TTL::: sub vcl_fetch { if (obj.ttl < 120s) { set obj.ttl = 120s; } } 下面這段程式碼用來強制快取帶cookies的內容: sub vcl_recv { if (req.request == "GET" && req.http.cookie) { call(lookup); } } sub vcl_fetch { if (beresp.http.Set-Cookie) { deliver; } } 下面程式碼的作用是利用squid的HTTP PURGE模式清理無法使用的目標。 acl purge { "localhost"; "192.0.2.1"/24; } sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } lookup; } } sub vcl_hit { if (req.request == "PURGE") { set obj.ttl = 0s; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { error 404 "Not in cache."; } } SEE ALSO Varnishd(1) HISTORY The VCL language was developed by Poul-Henning Kamp in cooperation with Verdens Gang AS, Linpro AS and Varnish Software. This manual page was written by Dag-Erling Smørgrav and later edited by Poul-Henning Kamp and Per Buer. COPYRIGHT 這個文檔的版權和varnish自身的版本一樣,請看LICENCE。 * Copyright (c) 2006 Verdens Gang AS * Copyright (c) 2006-2008 Linpro AS * Copyright (c) 2008-2010 Redpill Linpro AS * Copyright (c) 2010 Varnish Software AS