1.varnish安装
下载varnish软件 (此处下载3.0.2版本)
- #wget http://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz
- #tar -xvf varnish-3.0.2.tar.gz
- #cd varnish-3.0.2
- #./configure --prefix=/usr/local/varnish
- #make &&make install
至此安装完成。
注:如果你在编译的vasnish中的bin目录没有发现varnishstat, varnishtop, varnishhist这个三个程序的话,是因为编译前没有安装与操作系统位数对应的ncurses-devel。
安装ncurses-devel
yum install ncurses-devel.x86_64
然后再次编译varnish 即可。
2.varnish配置
#vim /usr/local/varnish/etc/varnish/vcl.conf
以下为配置代码:(除backend 及vcl_recv外,其他地方无需更改,这里是更改其访问的主机名,及定义后端服务器地址,字数有限,其他代码这里就不解释了)
- #backend vhost
- backend 192.168.40.85 {
- .host = "192.168.40.85";
- .port = "80";
- }
- #acl
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.0.0"/24;
- }
- sub vcl_recv {
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {
- remove req.http.Accept-Encoding;
- remove req.http.Cookie;
- } else if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- remove req.http.Accept-Encoding;
- }
- }
- if (req.http.host ~ "(.*)youdomain.com") {
- set req.backend = 192.168.40.85;
- }
- else {
- error 404 "This website is maintaining or not exist!";
- }
- if (req.request == "PURGE") {
- if (!client.ip ~purge) {
- error 405 "Not Allowed";
- }
- #.dd.....
- return(lookup);
- }
- #...GET...url...jpg,png,gif. ..cookie
- if (req.request == "GET"&& req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm|gz|tgz|bz2|tbz|mp3|ogg|mp4|flv|f4v|pdf)$") {
- unset req.http.cookie;
- }
- #..GET...url.php....cache....
- if (req.request =="GET"&&req.url ~ "\.php($|\?)"){
- return (pass);
- }
- # 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;
- }
- # }
- #........pipe..
- if (req.request != "GET" &&
- req.request != "HEAD" &&
- req.request != "PUT" &&
- req.request != "POST" &&
- req.request != "TRACE" &&
- req.request != "OPTIONS" &&
- req.request != "DELETE") {
- return (pipe);
- }
- #..GET .HEAD.....
- if (req.request != "GET" && req.request != "HEAD") {
- return (pass);
- }
- if (req.http.Authorization) {
- return (pass);
- }
- return (lookup);
- }
- #..url+host hash......
- sub vcl_hash {
- hash_data(req.url);
- if (req.http.host) {
- hash_data(req.http.host);
- } else {
- hash_data(server.ip);
- }
- return (hash);
- }
- # .....purge .....
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 200 "Purged";
- }
- return (deliver);
- }
- sub vcl_fetch {
- if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {
- set beresp.ttl = 2d;
- set beresp.http.expires = beresp.ttl;
- set beresp.http.Cache-Control = "max-age=172800";
- unset beresp.http.set-cookie;
- }
- if (req.url ~ "\.(dmg|js|css|html|htm)$") {
- set beresp.do_gzip = true;
- }
- if (beresp.status == 503) {
- set beresp.saintmode = 15s;
- }
- }
- sub vcl_deliver {
- set resp.http.x-hits = obj.hits ;
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT You!";
- } else {
- set resp.http.X-Cache = "MISS Me!";
- }
- }
配置文件保存退出 ,并启动 varnish,如下图
/usr/local/varnish3/sbin/varnishd -f /usr/local/varnish3/etc/varnish/default.vcl -a 192.168.40.84:8080 -u www -g www -p thread_pool_max=51200 -
p thread_pools=4 -h classic,500009 -p lru_interval=20 -s file,/a8root/varnish/varnish_cache.data,512M -w 1000,51200,10 -T 192.168,40.84:3500