当前位置:首页 > 技术文章 > 系统架构 > 系统架构-varnish安装与配置
系统架构-varnish安装与配置
文章来源:本站原创  浏览次数:194  发布日期:2013-07-19

//创建用户组和用户 varnish

#groupadd varnish
#useradd -g varnish varnish -s /usr/sbin/nologin



//创建目录

#mkdir /data/varnish
#mkdir /data/varnish/cache
#mkdir /data/varnish/log
#mkdir /usr/web/varnish



//设置目录权限

#chown -R varnish:varnish /usr/web/varnish
#chown -R varnish:varnish /data/varnish




//安装PCRE库

#mkdir /usr/web/pcre
#yum -y install unzip
#cd /tmp
#unzip pcre-8.33.zip
#cd /tmp/pcre-8.33
#./configure --prefix=/usr/web/pcre
#make && make install




//安装VARNISH    

#cd /tmp
#tar -zxvf varnish-3.0.4.tar.gz
#cd /tmp/varnish-3.0.4
#export PKG_CONFIG_PATH=/usr/web/pcre/lib/pkgconfig
#./configure --prefix=/usr/web/varnish \
--enable-werror \
--enable-debugging-symbols \
--enable-developer-warnings
#make && make install
////////////////
#cp redhat/varnish.initrc /etc/init.d/varnish
#cp redhat/varnish.sysconfig /etc/sysconfig/varnish
///////////////
#ln -fs /usr/web/varnish/bin/* /usr/bin/
#ln -fs /usr/web/varnish/sbin/varnishd /usr/sbin/varnishd



//配置文件修改


//开启防火墙 8090 端口

#vi /etc/sysconfig/iptables



//启动 第一种放到内存,第二种放到文件

#varnishd -f /usr/web/varnish/etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:8090
#varnishd -f /usr/web/varnish/etc/varnish/default.vcl -s file,/data/varnish/cache,1G -T 127.0.0.1:2000 -a 0.0.0.0:8090


//停止

#pkill varnishd
 
#ps -ef|grep varnish



//确定出后端命中最多的URL

#varnishtop –i txurl



//缓存状态

#varnishstat -n /data/varnish/cache



//清除缓存

#varnishadm -T 192.168.12.246:3500 purge.url <regexp>



//最近清除的列表

#varnishadm -T 192.168.12.246:3500 purge.list






//内核优化

net.ipv4.ip_local_port_range = 1024 65536 
net.core.rmem_max=16777216 
net.core.wmem_max=16777216 
net.ipv4.tcp_rmem=4096 87380 16777216 
net.ipv4.tcp_wmem=4096 65536 16777216 
net.ipv4.tcp_fin_timeout = 30 
net.core.netdev_max_backlog = 30000。 
net.ipv4.tcp_no_metrics_save=1 
net.core.somaxconn = 262144 
net.ipv4.tcp_syncookies = 1 
net.ipv4.tcp_max_orphans = 262144 
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_synack_retries = 2 
net.ipv4.tcp_syn_retries = 2





//图片防盗链 只需在配置文件的“vcl_recv”函数内增加如下配置

if (req.http.referer ~ "http://.*") { 
    if ( !(req.http.referer ~ "http://.*ixdba\.net"
    || req.http.referer ~ "http://.*google\.com"
    || req.http.referer ~ "http://.*yahoo\.cn"et 
    || req.http.referer ~ "http://.*google\.cn"
    )) { 
        set req.http.host = "www.ixdba.net"; 
        set req.url = "/templets/default/images/logo.gif";
    } 
    return (lookup); 
}



//Varnish实现静态文件压缩处理 只需在配置文件的“vcl_recv”函数内增加如下配置

if (req.http.Accept-Encoding) { 
    if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { 
        remove req.http.Accept-Encoding; 
    } 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; 
    } 
}


//然后修改“vcl_hash”函数为如下配置

sub vcl_hash { 
    set req.hash += req.url; 
    if (req.http.Accept-Encoding ~ "gzip") { 
        set req.hash += "gzip"; 
    }else if (req.http.Accept-Encoding ~ "deflate") { 
        set req.hash += "deflate"; 
    } 
    return (hash);
}


原文来自:系统架构-varnish安装与配置 http://www.redyun.net/technology/113.html

红云案例Case