生活随笔
收集整理的這篇文章主要介紹了
varnish-cache使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
Varnish Cache是一個web加速軟件,用作web服務加速的反向代理,與Squid不同的是它建立在較新的系統內核調用上,并且主要是使用內存作為緩存,它現有的使用者有facebook等,據使用者反饋,其與Squid相比,相同的訪問量下連接數大大減少。
本人測試過程
準備一個普通的HTTP web服務器,我在虛擬機內啟動了一個Linux+Apache+MySQL+Php環境,配置文件未改動,下載一個PHPWind 的bbs程序拿來測試。在另外一個服務器上編譯安裝Varnish 3.0(IP:192.168.159.5),默認安裝路徑,安裝過程可參考官方文檔。編輯Varnish的默認配置文件(/usr/local/etc/varnish/default.vcl): varnish ACL配置文件 #首先設置一個后端服務器
backend default {.host = "192.168.159.11";.port = "80";
}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);}#只有GET與HEAD方法才會使用Lookup,使用緩存。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);# }#如果請求的是php頁面直接轉發到后端服務器if (req.url ~ "\.(php|cgi)($|\?)") {return (pass);}return (lookup);}sub vcl_pipe {return (pipe);}sub vcl_pass {return (pass);}sub vcl_hash {hash_data(req.url);if (req.http.host) {hash_data(req.http.host);} else {hash_data(server.ip);}return (hash);}sub vcl_hit {return (deliver);}sub vcl_miss {return (fetch);}sub vcl_fetch {if (beresp.ttl <= 0s ||beresp.http.Set-Cookie ||beresp.http.Vary == "*") {/** Mark as "Hit-For-Pass" for the next 2 minutes*/set beresp.ttl = 120 s;return (hit_for_pass);}if (req.url ~ "\.(png|gif|jpg)$") {unset beresp.http.set-cookie;set beresp.ttl = 1h;}#設置圖片的緩存TTL為一小時return (deliver);}sub vcl_deliver {return (deliver);}sub vcl_error {set obj.http.Content-Type = "text/html; charset=utf-8";set obj.http.Retry-After = "5";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><p>Varnish cache server</p></body></html>"};return (deliver);}sub vcl_init {return (ok);}sub vcl_fini {return (ok);}
# 添加Varnishd進程用戶www,用戶組www,創建/var/vcache目錄,使www用戶有權限可讀寫。 groupadd www
useradd www -g www
mkdir /var/vcache
chown -R www:www /var/vcache
chmod -R 750 /var/vcache 編輯/etc/sysctl.conf 優化幾個內核參數: net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1 運行sysctl -p 重新按配置文件設置內核參數。
啟動Varnishd varnishd -a 0.0.0.0:80 -f /usr/local/etc/varnish/default.vcl -T 127.0.0.1:2000 -s file,/var/vcache/,1G -u www 參數說明:-f指定了配置文件,-T是指定命令行管理界面監聽地址,-s file指定了使用文件做緩存,1G是緩存文件大小,-u就是進程的用戶了。
在客戶端訪問http://192.168.159.5/phpwind?,高頻率刷新頁面觀察varnishd一端netstat -n輸出,可以發現Varnish端到后端(apache)的TCP連接幾乎一閃而過,很快就釋放掉。解決后端服務器不能日志記錄真實訪問者IP的問題,修改apache日志格式。 LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" varnish_combined ?
總結
以上是生活随笔為你收集整理的varnish-cache使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。