- 论坛徽章:
- 0
|
#======================= Varnish安装 =========================#
如果是RedHat/CentOS系统,在安装varnish的时候首先要安装以下软件包
automake
autoconf
libtool
ncurses-devel
libxslt
groff
pcre-devel
pkgconfig- groupadd www
- useradd www -g www -s /sbin/nologin
- mkdir -p /data/varnish/{cache,logs}
- chmod +w /data/varnish/{cache,logs}
- chown -R www:www /data/varnish/{cache,logs}
- cd /opt
- yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
- wget http://sourceforge.net/projects/varnish/files/varnish/2.1.3/varnish-2.1.3.tar.gz/download
- tar -zxvf varnish-2.1.3.tar.gz
- cd varnish-2.1.3
- ./configure --prefix=/usr/local/varnish
- make;make install
复制代码 #====================== varnish配置 ========================#- vi /usr/local/varnish/etc/varnish/kerry.vcl
复制代码 注意:在2.1后的版本里,原"obj.*"的变量全部变为"beresp.*"了,需要留意一下
启动varnish- /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/kerry.vcl -a 192.168.9.201:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.9.201:3000
复制代码- echo "/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/kerry.vcl -a 192.168.9.201:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.9.201:3000" >> /etc/rc.local
复制代码 参数:
-u 以什么用运行
-g 以什么组运行
-f varnish配置文件
-a 绑定IP和端口
-s varnish缓存文件位置与大小
-w 最小,最大线程和超时时间
-T varnish管理端口,主要用来清除缓存
-p client_http11=on 支持http1.1协议
-P(大P) /usr/local/varnish/var/varnish.pid 指定其进程码文件的位置,实现管理
停止varnish
pkill varnishd #结束varnishd进程
启动日志,方便分析网站访问情况- /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &
复制代码- echo "/usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &" >> /etc/rc.local
复制代码 参数: -w 指定varnish访问日志要写入的目录与文件
varnish日志切割- vi /root/cut_varnish_log.sh
复制代码- #!/bin/sh
- logs_path=/data/varnish/logs
- vlog=${logs_path}/varnish.log
- date=$(date -d "yesterday" +"%Y-%m-%d")
- pkill -9 varnishncsa
- mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
- mv /data/varnish/logs/varnish.log ${logs_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/varnish-${date}.log
复制代码- /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &
复制代码 使用计划任务,每天晚上凌晨00点运行日志切割脚本- echo "0 0 * * * /root/cut_varnish_log.sh" >> /etc/crontab
复制代码- ulimit -SHn 51200
- /usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/kerry.vcl -a 192.168.9.201:80 -s file,/data/varnish/cache/varnish_cache.data,1G -w 1024,51200,10 -t 3600 -T 192.168.9.201:3000
- /usr/local/varnish/bin/varnishncsa -w /data/varnish/logs/varnish.log &
复制代码 #======================== Varnish 缓存清除 ====================#- /usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 purge "req.http.host ~ www.kerry.com$ && req.url ~ /static/image/tp.php"
复制代码 说明:
192.168.9.201:3000 为被清除缓存服务器地址
www.kerry.com 为被清除的域名
/static/image/tp.php 为被清除的url地址列表
清除所有缓存- /usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge *$
复制代码 清除image目录下所有缓存- /usr/local/varnish/bin/varnishadm -T 192.168.9.201:3000 url.purge /image/
复制代码 查看Varnish服务器连接数与命中率- /usr/local/varnish/bin/varnishstat –n /data/varnish/cache/varnish_cache.data
复制代码 #======================= 内核优化 =========================#- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- #net.ipv4.tcp_fin_timeout = 30
- #net.ipv4.tcp_keepalive_time = 300
- net.ipv4.ip_local_port_range = 1024 65000
- net.ipv4.tcp_max_syn_backlog = 8192
- net.ipv4.tcp_max_tw_buckets = 5000
-
- net.ipv4.tcp_max_syn_backlog = 65536
- net.core.netdev_max_backlog = 32768
- net.core.somaxconn = 32768
-
- net.core.wmem_default = 8388608
- net.core.rmem_default = 8388608
- net.core.rmem_max = 16777216
- net.core.wmem_max = 16777216
-
- net.ipv4.tcp_timestamps = 0
- net.ipv4.tcp_synack_retries = 2
- net.ipv4.tcp_syn_retries = 2
-
- net.ipv4.tcp_tw_recycle = 1
- #net.ipv4.tcp_tw_len = 1
- net.ipv4.tcp_tw_reuse = 1
-
- net.ipv4.tcp_mem = 94500000 915000000 927000000
- net.ipv4.tcp_max_orphans = 327680
复制代码 0#=================== Varnish添加到服务自启动 ====================#
配置启动文件varnish的配置调用文件,是用来告诉程序从哪里读取配置文件,启动参数有哪些等- vi /usr/local/varnish/etc/varnish
复制代码- # Configuration file for varnish
- #
- # /etc/init.d/varnish expects the variable $DAEMON_OPTS to be set from this
- # shell script fragment.
- #
-
- # Maximum number of open files (for ulimit -n)
- NFILES=131072
-
- # Locked shared memory (for ulimit -l)
- # Default log size is 82MB + header
- MEMLOCK=1000000
-
- ## Alternative 2, Configuration with VCL
- DAEMON_OPTS="-a 192.168.9.201:80 \
- -f /usr/local/varnish/etc/varnish/kerry.vcl \
- -T 192.168.9.201:3000 \
- -u www -g www \
- -n /data/varnish/cache \
- -s file,/data/varnish/cache/varnish_cache.data,1G"
复制代码 添加到系统服务,开机自启动- chmod +x /etc/init.d/varnish
- /sbin/chkconfig --add varnish
- /sbin/chkconfig --level 2345 varnish on
复制代码 开启varnish- /etc/init.d/varnish start
复制代码 关闭varnish |
|