本篇內(nèi)容主要講解“varnish怎么配置”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“varnish怎么配置”吧!
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營(yíng)銷推廣、網(wǎng)站重做改版、芷江網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為芷江等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。server1:172.25.254.20 varnish
server2:172.25.254.21 http
主機(jī):172.25.254.44
Varnish是一款高性能的開源HTTP加速器
在server1上:
#yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
# vim /etc/varnish/default.vcl
backend web1 {
.host = "127.0.0.1";
.port = "8080";
} #定義本機(jī)端口
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from westos cache";
}
else {
set resp.http.X-Cache = "MISS from westos cache";
}
return (deliver);
} #定義是否命中
# yum install httpd -y
# cd /var/www/html
# echo server20.example.com > index.html
# vim /etc/httpd/conf/httpd.conf
# Listen 12.34.56.78:80
Listen 8080 #修改http端口,防止和varnish端口沖突
# /etc/init.d/httpd start
# netstat -antlp #查看http 8080 端口是否開啟
# vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 # varnish端口
# /etc/init.d/varnish start
[root@server20 html]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination #查看本機(jī)火墻是否關(guān)閉
[root@foundation44 Desktop]# curl -I 172.25.254.20
HTTP/1.1 404 westos cache
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 398
Accept-Ranges: bytes
Date: Wed, 29 Jun 2016 10:11:45 GMT
X-Varnish: 1112141971
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: MISS from westos cache #此時(shí)未命中,說(shuō)明還沒有緩存
[root@foundation44 Desktop]# curl -I 172.25.254.20
HTTP/1.1 404 westos cach
Server: Varnish
Content-Type: text/html; charset=utf-8
Retry-After: 5
Content-Length: 398
Accept-Ranges: bytes
Date: Wed, 29 Jun 2016 10:15:02 GMT
X-Varnish: 1112141972
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: HIT from westos cache #此時(shí)命中,說(shuō)明已經(jīng)有緩存
# vim /etc/varnish/default.vcl
backend web2 {
.host = "172.25.254.21";
.port = "80";
}
director lb random { #把多個(gè)后端聚合在一起
{ .backend = web1;
.weight = 1;
}
{ .backend = web2;
.weight = 2;
}
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?westos.org") {
set req.http.host = "www.westos.org";
set req.backend = lb;
return(pass); #為了方便,所以定義不緩存
} elsif (req.http.host ~ "^bbs.westos.org") {
set req.backend = web2;
} else {
error 404 "westos cache";
}
} #如果訪問(wèn)其他會(huì)報(bào)錯(cuò) 404
# /etc/init.d/varnish reload
在server2上進(jìn)行的操作
yum install httpd -y
# cd /var/www/html
# echo server21.example.com > index.html
# /etc/init.d/httpd start
# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
DocumentRoot /var/www/html
ServerName www.westos.org
serveralias westos.org
DocumentRoot /var/www/virtualhost/bbs
ServerName bbs.westos.org
# mkdir -p /var/www/virtualhost/bbs
# cd /var/www/virtualhost/bbs/
# echo bbs.example.com > index.html
# /etc/init.d/httpd restart
# vim /etc/httpd/conf/httpd.conf
# /etc/init.d/httpd restart
然后在主機(jī)上加上 server1上和server2上相關(guān)的解析,在主機(jī)上火狐訪問(wèn),或者用 curl -I www.westos.org
到此,相信大家對(duì)“varnish怎么配置”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。