varnish安装

简介:

 yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig

yum -y install cmake flex byacc libmcrypt libmcrypt-devel libmhash libmhash-devel patch make gcc gcc-c++ libtool libtool-libs libart_lgpl libart_lgpl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel fontconfig fontconfig-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
 
wget http://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz
tar -zxvf varnish-3.0.2.tar.gz
cd varnish-3.0.2
./configure --prefix=/usr/local/varnish
make && make install
cp redhat/varnish.initrc /etc/init.d/varnish
chmod 755 /etc/init.d/varnish
cd ..
 
sed -i 's#/usr/sbin/varnishd#/usr/local/varnish/sbin/varnishd#g' /etc/init.d/varnish 
 
groupadd cache -g 4848
useradd -u 4848 -g cache cache
mkdir -p /var/logs
chmod +w /var/logs
chown -R cache:cache /var/logs
 
vim /etc/sysconfig/varnish
NFILES=65535
MEMLOCK=82000
 
DAEMON_OPTS="-u cache -g cache -f /usr/local/varnish/etc/varnish/vcl.conf -n /var/logs/cache -a 0.0.0.0:80 -s malloc,1G -p thread_pools=2 -p listen_depth=4096 -p first_byte_timeout=10 -p between_bytes_timeout=60 -p sess_timeout=15 -T 127.0.0.1:4848"
 
 
 
 
vim vcl.conf
 
backend wwwserver {
       .host = "192.168.2.103";
       .port = "80";
}
backend staticserver {
  .host = "192.168.2.103";
  .port = "80";
}
acl purge {
       "localhost";
       "127.0.0.1";
       "192.168.1.0"/24;
}
 
sub vcl_recv {
       if (req.request == "PURGE") {
               if (!client.ip ~ purge) {
                       error 405 "Not allowed.";
               }
               return(lookup);
       }
#del cookie
if (req.request == "GET" && req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip|html)(\?.*|)$") {
unset req.http.Cookie;
}
 
# req.http.X-Forwarded-For
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;
}
 
#Accept-Encoding 
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|jpeg)$") {
    remove req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
    set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
    set req.http.Accept-Encoding = "deflate";
} else {
    remove req.http.Accept-Encoding;
}
}
 
if (req.http.host ~ "^www.akin.com") {
               set req.backend = wwwserver;
               if (req.request != "GET" && req.request != "HEAD") {
                       return(pipe);
               }
                elseif(req.url ~ "\.(php|cgi|jsp|do|htm)($|\?)") {
                return(pass);
                }
               else {
                       return(lookup);
               }
       } 
 
 
elsif (req.http.host == "192.168.2.101" ) {
        set req.backend = staticserver;
        }
 
else {
error 404 "Cache Server";
        return(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.";
       }
}
 
sub vcl_fetch {
       if (req.request == "GET" && req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip)$") {
               set beresp.ttl = 600s;
unset beresp.http.set-cookie;
       }
       else {
               set beresp.ttl = 3600s;
       }
}
 
#http head
sub vcl_deliver{
if (obj.hits > 0) {
set resp.http.X-Cache = "CDN-HIT";
set resp.http.X-Cache-Hits = obj.hits;
        } else {
                set resp.http.X-Cache = "CDN-MISS";
        }
unset resp.http.X-Varnish;
        set resp.http.Via = "1.1 Xcache";
}
 
 
/etc/init.d/varnish start
 
 
 
 
 
我们可以通过varnishadm这个命令从管理端口进行指定缓存的清除
\/usr/local/varnish/bin/varnishlog -n /data/varnish/cache  
 
/usr/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /test/*
 
/usr/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$ (清除所有缓存)
 
 
http://blog.c1gstudio.com/archives/1272
https://www.varnish-software.com/blog/virtual-hosts-varnish
 

本文转自 liang3391 51CTO博客,原文链接:http://blog.51cto.com/liang3391/812380
相关文章
|
1月前
|
存储 缓存 运维
LAMP+Varnish缓存详解(一)——Varnish简介
LAMP+Varnish缓存详解(一)——Varnish简介
18 0
|
存储 缓存 监控
|
存储 Web App开发 缓存
|
Web App开发 缓存 测试技术
|
Web App开发 存储 缓存
|
缓存 前端开发 区块链