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
./configure --prefix=/usr/local/varnish
cp redhat/varnish.initrc /etc/init.d/varnish
chmod 755 /etc/init.d/varnish
sed -i 's#/usr/sbin/varnishd#/usr/local/varnish/sbin/varnishd#g' /etc/init.d/varnish
useradd -u 4848 -g cache cache
chown -R cache:cache /var/logs
vim /etc/sysconfig/varnish
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"
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
if (req.request == "GET" && req.url ~ "^/[^?]+\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip|html)(\?.*|)$") {
# req.http.X-Forwarded-For
if (req.http.x-forwarded-for)
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", "+ client.ip;
set req.http.X-Forwarded-For = client.ip;
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";
remove req.http.Accept-Encoding;
if (req.http.host ~ "^www.akin.com") {
set req.backend = wwwserver;
if (req.request != "GET" && req.request != "HEAD") {
elseif(req.url ~ "\.(php|cgi|jsp|do|htm)($|\?)") {
elsif (req.http.host == "192.168.2.101" ) {
set req.backend = staticserver;
error 404 "Cache Server";
if (req.request == "PURGE") {
if (req.request == "PURGE") {
error 404 "Not in cache.";
if (req.request == "GET" && req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|txt|zip)$") {
unset beresp.http.set-cookie;
set resp.http.X-Cache = "CDN-HIT";
set resp.http.X-Cache-Hits = obj.hits;
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