varnish 緩存是 web 應(yīng)用加速器,同時(shí)也作為 http 反向緩存代理。你可以安裝 varnish 在任何
http 的前端,同時(shí)配置它緩存內(nèi)容。與傳統(tǒng)的 squid 相比,varnish 具有性能更高、速度更
快、管理更加方便等諸多優(yōu)點(diǎn)。
varnish 完整配置實(shí)例
1、拓?fù)洵h(huán)境
Varnish:192.168.31.250
Web01:192.168.31.83
Web02:192.168.31.141
配置 web01、web02 做為后端服務(wù)器(過(guò)程略)
確保 varnish 服務(wù)器能正常訪(fǎng)問(wèn) web01、web02
Varnish 緩存代理服務(wù)器配置:
安裝 varnish
1、安裝依賴(lài)關(guān)系的軟件包(注:使用 centos 在線(xiàn) yum 源)
[root@varnish ~]# yum -y install autoconf automake libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
2、安裝 varnish
Varnish 的官方網(wǎng)址為 http://varnish-cache.org,可以在這里下載最新版本的軟件。
下載地址:https://www.varnish-cache.org/content/varnish-cache-403
注意:Varnish 網(wǎng)站有時(shí)會(huì)被墻。
Git 下載:git clone https://github.com/varnish/Varnish-Cache /var/tmp/
解壓,進(jìn)入解壓目錄編譯安裝:
[root@varnish ~]# tar zxf varnish-4.0.3.tar.gz
[root@varnish ~]# cd varnish-4.0.3/
[root@varnish varnish-4.0.3]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
注:
./autogen.sh
如果從 Git 庫(kù)下載的安裝包時(shí)才需要運(yùn)行,用于生成 configure 編譯文件。
配置:
[root@varnish varnish-4.0.3]# ./configure
注:不指定安裝路徑,默認(rèn)是安裝在/usr/local 目錄下
編譯、安裝
[root@varnish varnish-4.0.3]# make && make install
復(fù)制 vcl 文件(在編譯安裝目錄下),如果安裝目錄里沒(méi)有 default.vcl 文件。
復(fù)制到安裝目錄的/usr/local/var/varnish/目錄下(當(dāng)然并無(wú)必需要求在哪個(gè)目錄,因?yàn)檎?br/>啟動(dòng)時(shí)還得指定這個(gè)文件的目錄)[root@varnish varnish-4.0.3]# cp etc/example.vcl /usr/local/var/varnish/default.vcl
配置default.vcl
[root@varnish varnish-4.0.3]# vim /usr/loacl/var/varnish/default.vcl
vcl 4.0;
import directors;
import std;
probe backend_healthcheck {
.url="/";
.interval = 5s;
.timeout = 1s;
.window = 5;
.threshold = 3;
}
backend web_app_01 {
.host = "192.168.164.10"; #要改
.port = "80";
.first_byte_timeout = 9s;
.connect_timeout = 3s;
.between_bytes_timeout = 1s;
.probe = backend_healthcheck;
}
backend web_app_02 {
.host = "192.168.164.20"; #要改
.port = "80";
.first_byte_timeout = 9s;
.connect_timeout = 3s;
.between_bytes_timeout = 1s;
.probe = backend_healthcheck;
}
acl purgers {
"127.0.0.1";
"localhost";
"192.168.164.0/24"; #添加網(wǎng)段
}
sub vcl_init {
new web = directors.round_robin();
web.add_backend(web_app_01);
web.add_backend(web_app_02);
}
sub vcl_recv {
set req.backend_hint = web.backend();
if (req.method == "PURGE") {
if (!client.ip ~ purgers) {
return (synth(405, "Not Allowed."));
}
return (purge);
}
if (req.method != "GET" &&req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "PATCH" &&
req.method != "DELETE") {
return (pipe);
}
if (req.method != "GET" && req.method != "HEAD") {
return (pass);
}
if (req.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {
return (pass);
}
if (req.http.Authorization) {
return (pass);
}
if (req.http.Accept-Encoding) {
if (req.url ~"\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
unset req.http.Accept-Encoding;
} elseif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elseif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
if (req.url ~"\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
unset req.http.cookie;
return (hash);
}
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;
}
}
return (hash);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (lookup);
}
sub vcl_hit {
if (req.method == "PURGE") {
return (synth(200, "Purged."));
}
return (deliver);
}
sub vcl_miss {
if (req.method == "PURGE") {
return (synth(404, "Purged."));
}
return (fetch);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
unset resp.http.X-Powered-By;
unset resp.http.Server;
unset resp.http.X-Drupal-Cache;
unset resp.http.Via;
unset resp.http.Link;
unset resp.http.X-Varnish;
set resp.http.xx_restarts_count = req.restarts;
set resp.http.xx_Age = resp.http.Age;
set resp.http.hit_count = obj.hits;
unset resp.http.Age;
return (deliver);
}
sub vcl_pass {
return (fetch);
}
sub vcl_backend_response {
set beresp.grace = 5m;
if (beresp.status == 499 || beresp.status == 404 || beresp.status == 502) {
set beresp.uncacheable = true;
}
if (bereq.url ~ "\.(php|jsp)(\?|$)") {
set beresp.uncacheable = true;
} else {
if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)") {
set beresp.ttl = 15m;
unset beresp.http.Set-Cookie;
} elseif (bereq.url ~ "\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {
set beresp.ttl = 30m;
unset beresp.http.Set-Cookie;
} else {
set beresp.ttl = 10m;
unset beresp.http.Set-Cookie;
}
}
return (deliver);
}
sub vcl_purge {
return (synth(200,"success"));
}
sub vcl_backend_error {
if (beresp.status == 500 ||
beresp.status == 501 ||
beresp.status == 502 ||
beresp.status == 503 ||
beresp.status == 504) {
return (retry);
}
}
sub vcl_fini {
return (ok);
}
**啟動(dòng) varnish**
[root@varnish ~]# /usr/local/sbin/varnishd -f /usr/local/var/varnish/default.vcl -s
malloc,200M -a 0.0.0.0:80
[root@varnish ~]# netstat -anpt | grep 80
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。