下載地址:http://varnish-cache.org/releases/
這裏用的是varnish-4.1.11版本
varnish4版本使用python2.6便可,varnish6版本要求python2.7css
visudo
註釋掉 Defaults requiretty 這行html
安裝:
安裝依賴包:
yum -y install autoconf automake jemalloc-devel libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
yum -y install gcc gcc-c++ openssl openssl-devel openssh-clients wget make lrzsz unzip zip xz ntpdate lsof telnet epel-release vim tree libselinux-pythonpython
cd /usr/local/src/ tar -zxf varnish-4.1.11.tgz cd varnish-4.1.11 ./configure --prefix=/home/tools/varnish make make install
[root@slave2 src]# cd /home/tools/varnish/ [root@slave2 varnish]# ll total 40 drwxr-xr-x 2 root root 4096 Jun 26 18:37 bin drwxr-xr-x 3 root root 4096 Jun 26 15:46 include drwxr-xr-x 4 root root 4096 Jun 26 15:46 lib drwxr-xr-x 2 root root 4096 Jun 26 15:46 sbin drwxr-xr-x 6 root root 4096 Jun 26 15:46 share drwxr-xr-x 3 root root 4096 Jun 27 09:59 var
建立日誌目錄:mkdir logs
linux
配置:
建立配置文件:
cat /home/tools/varnish/default.vclc++
vcl 4.0; # 加載後端輪詢模塊 import directors; # 建立健康監測 probe backend_healthcheck { .url = "/health.html"; .window = 5; .threshold = 2; .interval = 5s; .timeout = 3s; } # 建立後端主機 backend web1 { .host = "192.168.1.223"; .port = "80"; .probe = backend_healthcheck; } backend web2 { .host = "192.168.1.223"; .port = "80"; .probe = backend_healthcheck; } backend img1 { .host = "192.168.1.223"; .port = "80"; .probe = backend_healthcheck; } backend img2 { .host = "192.168.1.223"; .port = "80"; .probe = backend_healthcheck; } # 建立後端主機組,基於round_robin輪轉 sub vcl_init { new web_cluster = directors.round_robin(); web_cluster.add_backend(web1); web_cluster.add_backend(web2); new img_cluster = directors.round_robin(); img_cluster.add_backend(img1); img_cluster.add_backend(img2); } # 定義PURGE方法訪問來源IP acl purgers { "127.0.0.1"; #"192.168.0.0"/24; } sub vcl_recv { # 帶cookie首部的GET請求也緩存 if (req.method == "GET" && req.http.cookie) { return(hash); } # health.html文件不緩存 if (req.url ~ "health.html") { return(pass); } # PURGE請求的處理 if (req.method == "PURGE") { if (!client.ip ~ purgers) { return(synth(405,"Method not allowed")); } return(hash); } # 爲發日後端主機的請求添加X-Forward-For首部 if (req.restarts == 0) { if (req.http.X-Fowarded-For) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } # 匹配不通的URL,分發至不一樣的後端主機組 if (req.url ~ "\.(jpg|jpeg|gif|png)$") { set req.backend_hint = img_cluster.backend(); } else { #} elseif (req.url ~ "\.(html|css|js)$") { set req.backend_hint = web_cluster.backend(); } set req.backend_hint = web_cluster.backend(); return(hash); } # PURGE請求的處理 sub vcl_hit { if (req.method == "PURGE") { return(synth(200,"Purged")); } } # PURGE請求的處理 sub vcl_miss { if (req.method == "PURGE") { return(synth(404,"Not in cache")); } } # PURGE請求的處理 sub vcl_pass { if (req.method == "PURGE") { return(synth(502,"PURGE on a passed object")); } } # 自定義緩存文件的緩存時長,即TTL值 sub vcl_backend_response { if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") { set beresp.ttl = 7200s; } if (bereq.url ~ "\.(html|css|js)$") { set beresp.ttl = 1200s; } # 定義帶Set-Cookie首部的後端響應不緩存,直接返回給客戶端 if (beresp.http.Set-Cookie) { return(deliver); } } # 爲響應添加X-Cache首部,顯示緩存是否命中 sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from " + server.ip; } else { set resp.http.X-Cache = "MISS from " + server.ip; } }
建立普通帳號:web
groupadd varnish useradd -s /sbin/nologin -g varnish varnish -M
受權varnish安裝目錄:chown -R varnish. /home/tools/varnish
正則表達式
[root@slave2 varnish]# ll total 44 drwxr-xr-x 2 varnish varnish 4096 Jun 26 18:37 bin -rw-r--r-- 1 varnish varnish 3099 Jun 26 18:52 default.vcl drwxr-xr-x 3 varnish varnish 4096 Jun 26 15:46 include drwxr-xr-x 4 varnish varnish 4096 Jun 26 15:46 lib drwxr-xr-x 2 varnish varnish 4096 Jun 27 12:12 logs drwxr-xr-x 2 varnish varnish 4096 Jun 26 15:46 sbin drwxr-xr-x 6 varnish varnish 4096 Jun 26 15:46 share drwxr-xr-x 3 varnish varnish 4096 Jun 27 09:59 var
命令啓動:
sudo -u varnish /home/tools/varnish/sbin/varnishd -f /home/tools/varnish/default.vcl -s malloc,1G -T 127.0.0.1:20000 -a 0.0.0.0:8888 -p thread_pool_min=50 -p thread_pool_max=1000 -P /home/tools/varnish/varnish.pid
-s:指定使用內存進行緩存,內存大小1G
-T:指定管理IP和端口
-a:指定訪問IP和端口
-p:指定單個pool打開的最小thread和最大thread(pool數通常與CPU核數一致)
-P:pid進程文件
命令中止:
pkill varnishshell
腳本啓動:
建立配置文件:
cat /etc/sysconfig/varnishexpress
# Configuration file for Varnish Cache # # /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=82000 # Maximum number of threads (for ulimit -u) NPROCS="unlimited" # Maximum size of corefile (for ulimit -c). Default in Fedora is 0 # DAEMON_COREFILE_LIMIT="unlimited" # Init script support to reload/switch vcl without restart. # To make this work, you need to set the following variables # explicit: VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_ADDRESS, # VARNISH_ADMIN_LISTEN_PORT, VARNISH_SECRET_FILE. RELOAD_VCL=1 # Set WARMUP_TIME to force a delay in reload-vcl between vcl.load and vcl.use # This is useful when backend probe definitions need some time before declaring # configured backends healthy, to avoid routing traffic to a non-healthy backend. #WARMUP_TIME=0 # Main configuration file. VARNISH_VCL_CONF=/home/tools/varnish/default.vcl # # Default address and port to bind to # Blank address means all IPv4 and IPv6 interfaces, otherwise specify # a host name, an IPv4 dotted quad, or an IPv6 address in brackets. VARNISH_LISTEN_PORT=8888 # # Telnet admin interface listen address and port VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 VARNISH_ADMIN_LISTEN_PORT=20000 # # Shared secret file for admin interface VARNISH_SECRET_FILE=/home/tools/varnish/secret # # The minimum number of worker threads to start VARNISH_MIN_THREADS=50 # # The Maximum number of worker threads to start VARNISH_MAX_THREADS=1000 # # Cache file size: in bytes, optionally using k / M / G / T suffix. VARNISH_STORAGE_SIZE=1G # # Backend storage specification VARNISH_STORAGE="malloc,${VARNISH_STORAGE_SIZE}" # # Default TTL used when the backend does not specify one VARNISH_TTL=120 # # DAEMON_OPTS is used by the init script. DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \ -f ${VARNISH_VCL_CONF} \ -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \ -p thread_pool_min=${VARNISH_MIN_THREADS} \ -p thread_pool_max=${VARNISH_MAX_THREADS} \ -S ${VARNISH_SECRET_FILE} \ -s ${VARNISH_STORAGE}"
建立secret安全文件:vim
[root@slave2 varnish]# cat /home/tools/varnish/secret 65c8db78-464e-4450-91c0-789b2a4f96a4
建立reload_vcl文件:
chmod +x /home/tools/varnish/bin/varnish_reload_vcl
cat /home/tools/varnish/bin/varnish_reload_vcl
#!/bin/bash # # reload vcl revisited # A script that loads new vcl based on data from /etc/sysconfig/varnish # Ingvar Hagelund <ingvar@redpill-linpro.com> # # This is free software, distributed under the standard 2 clause BSD license, # see the LICENSE file in the Varnish documentation directory # # The following environment variables have to be set: # RELOAD_VCL, VARNISH_VCL_CONF, VARNISH_ADMIN_LISTEN_PORT # The following are optional: # VARNISH_SECRET_FILE, VARNISH_ADMIN_LISTEN_ADDRESS # # Requires GNU bash and GNU date # debug=false missing() { echo "Missing configuration variable: $1" exit 2 } print_debug() { echo " Parsed configuration: RELOAD_VCL=\"$RELOAD_VCL\" VARNISH_VCL_CONF=\"$VARNISH_VCL_CONF\" VARNISH_ADMIN_LISTEN_ADDRESS=\"$VARNISH_ADMIN_LISTEN_ADDRESS\" VARNISH_ADMIN_LISTEN_PORT=\"$VARNISH_ADMIN_LISTEN_PORT\" VARNISH_SECRET_FILE=\"$VARNISH_SECRET_FILE\" " } # Fallback to the default configuration if [ -z "$VARNISH_VCL_CONF" ]; then . /etc/sysconfig/varnish fi # Get warmup time from defaults or use 0 warmup WARMUP=${WARMUP_TIME:-0} $debug && print_debug # Check configuration if [ ! "$RELOAD_VCL" = "1" ]; then echo "Error: RELOAD_VCL is not set to 1" exit 2 elif [ -z "$VARNISH_VCL_CONF" ]; then echo "Error: VARNISH_VCL_CONF is not set" exit 2 elif [ ! -s "$VARNISH_VCL_CONF" ]; then echo "Eror: VCL config $VARNISH_VCL_CONF is unreadable or empty" exit 2 elif [ -z "$VARNISH_ADMIN_LISTEN_ADDRESS" ]; then echo "Warning: VARNISH_ADMIN_LISTEN_ADDRESS is not set, using 127.0.0.1" VARNISH_ADMIN_LISTEN_ADDRESS="127.0.0.1" elif [ -z "$VARNISH_ADMIN_LISTEN_PORT" ]; then echo "Error: VARNISH_ADMIN_LISTEN_PORT is not set" exit 2 elif [ -z "$VARNISH_SECRET_FILE" ]; then echo "Warning: VARNISH_SECRET_FILE is not set" secret="" elif [ ! -s "$VARNISH_SECRET_FILE" ]; then echo "Error: varnish secret file $VARNISH_SECRET_FILE is unreadable or empty" exit 2 else secret="-S $VARNISH_SECRET_FILE" fi # Done parsing, set up command VARNISHADM="/home/tools/varnish/bin/varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT" # Now do the real work new_config="reload_$(date +%FT%H%M%S)" # Check if we are able to connect at all if $VARNISHADM vcl.list > /dev/null; then $debug && echo vcl.list succeeded else echo "Unable to run $VARNISHADM vcl.list" exit 1 fi if $VARNISHADM vcl.list | awk ' { print $4 } ' | grep -q $new_config; then echo Trying to use new config $new_config, but that is already in use exit 2 fi current_config=$( $VARNISHADM vcl.list | awk ' /^active/ { print $4 } ' ) echo "Loading vcl from $VARNISH_VCL_CONF" echo "Current running config name is $current_config" echo "Using new config name $new_config" if $VARNISHADM vcl.load $new_config $VARNISH_VCL_CONF; then $debug && echo "$VARNISHADM vcl.load succeded" else echo "$VARNISHADM vcl.load failed" exit 1 fi # Wait before vcl.use if WARMUP > 0 # Used if backend probes require some time to set backends healthy sleep $WARMUP if $VARNISHADM vcl.use $new_config; then $debug && echo "$VARNISHADM vcl.use succeded" else echo "$VARNISHADM vcl.use failed" exit 1 fi $VARNISHADM vcl.list echo Done exit 0
chmod +x /home/tools/varnish/bin/varnish_reload_vcl
建立腳本文件:
cat /etc/init.d/varnish
#!/bin/sh # # Init script for Varnish Cache. # # chkconfig: - 90 10 # description: Varnish is a high-performance HTTP accelerator # processname: varnishd # config: /etc/sysconfig/varnish # pidfile: /var/run/varnish.pid ### BEGIN INIT INFO # Provides: varnish # Required-Start: $network $local_fs $remote_fs # Required-Stop: $network $local_fs $remote_fs # Default-Start: # Default-Stop: # Should-Start: $syslog # Short-Description: start and stop varnishd # Description: Varnish is a high-performance HTTP accelerator ### END INIT INFO # Source function library. . /etc/init.d/functions retval=0 pidfile=/home/tools/varnish/varnish.pid exec="/home/tools/varnish/sbin/varnishd" reload_exec="/home/tools/varnish/bin/varnish_reload_vcl" prog="varnishd" config="/etc/sysconfig/varnish" lockfile="/var/lock/subsys/varnish" # Include varnish defaults [ -e /etc/sysconfig/varnish ] && . /etc/sysconfig/varnish start() { if [ ! -x $exec ] then echo $exec not found exit 5 fi if [ ! -f $config ] then echo $config not found exit 6 fi echo -n "Starting Varnish Cache: " # Open files (usually 1024, which is way too small for varnish) ulimit -n ${NFILES:-131072} # Varnish wants to lock shared memory log in memory. ulimit -l ${MEMLOCK:-82000} # Maximum number of threads (default in CentOS is 1024, which # is often too small for varnish) ulimit -u ${NPROCS:-unlimited} # If defined, set maximum core size. if [ -n "${DAEMON_COREFILE_LIMIT}" ] then ulimit -c ${DAEMON_COREFILE_LIMIT} fi # $DAEMON_OPTS is set in /etc/sysconfig/varnish. if [ "$DAEMON_OPTS" = "" ]; then echo "\$DAEMON_OPTS is empty." echo -n "Please put configuration options in $config" return 6 else daemon --pidfile $pidfile sudo -u varnish $exec "$DAEMON_OPTS" -P $pidfile retval=$? if [ $retval -eq 0 ] then touch $lockfile echo_success echo else echo_failure echo fi return $retval fi } stop() { echo -n "Stopping Varnish Cache: " killproc -p $pidfile $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { if [ "$RELOAD_VCL" = "1" ] then $reload_exec else force_reload fi } force_reload() { restart } rh_status() { status -p $pidfile $prog } rh_status_q() { rh_status >/dev/null 2>&1 } configtest() { if [ -f "$VARNISH_VCL_CONF" ]; then $exec -C $DAEMON_OPTS -n /tmp 2>/dev/null if [ "$?" = 0 ]; then echo "Syntax ok" else $exec -C $DAEMON_OPTS -n /tmp return $? fi else echo "VARNISH_VCL_CONF is unset or does not point to a file." fi } # See how we were called. case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; configtest) configtest ;; *) echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
chmod +x /etc/init.d/varnish
chkconfig varnish on
[root@slave2 init.d]# /etc/init.d/varnish start Starting Varnish Cache: [ OK ] [root@slave2 init.d]# [root@slave2 init.d]# ps -ef |grep varnish varnish 23100 1 0 15:16 ? 00:00:00 /home/tools/varnish/sbin/varnishd -a :8888 -f /home/tools/varnish/default.vcl -T 127.0.0.1:20000 -p thread_pool_min=50 -p thread_pool_max=1000 -S /home/tools/varnish/secret -s malloc,1G -P /home/tools/varnish/varnish.pid varnish 23102 23100 2 15:16 ? 00:00:00 /home/tools/varnish/sbin/varnishd -a :8888 -f /home/tools/varnish/default.vcl -T 127.0.0.1:20000 -p thread_pool_min=50 -p thread_pool_max=1000 -S /home/tools/varnish/secret -s malloc,1G -P /home/tools/varnish/varnish.pid root 23220 3849 0 15:16 pts/0 00:00:00 grep varnish
[root@slave2 init.d]# /etc/init.d/varnish reload Loading vcl from /home/tools/varnish/default.vcl Current running config name is boot Using new config name reload_2019-06-27T152105 VCL compiled. VCL 'reload_2019-06-27T152105' now active available auto/warm 0 boot active auto/warm 0 reload_2019-06-27T152105 Done
[root@slave2 ~]# /home/tools/varnish/bin/varnishadm -T 127.0.0.1:20000 -S /home/tools/varnish/secret backend.list Backend name Admin Probe reload_2019-06-27T152105.web1 probe Healthy 5/5 reload_2019-06-27T152105.web2 probe Healthy 5/5 reload_2019-06-27T152105.img1 probe Healthy 5/5 reload_2019-06-27T152105.img2 probe Healthy 5/5 reload_2019-06-27T152105.img3 probe Healthy 5/5
[root@slave2 ~]# /home/tools/varnish/bin/varnishadm -T 127.0.0.1:20000 -S /home/tools/varnish/secret help help [<command>] ping [<timestamp>] auth <response> quit banner status start stop vcl.load <configname> <filename> [auto|cold|warm] vcl.inline <configname> <quoted_VCLstring> [auto|cold|warm] vcl.use <configname> vcl.state <configname> [auto|cold|warm] vcl.discard <configname> vcl.list param.show [-l] [<param>] param.set <param> <value> panic.show panic.clear [-z] storage.list vcl.show [-v] <configname> backend.list [-p] [<backend_expression>] backend.set_health <backend_expression> <state> ban <field> <operator> <arg> [&& <field> <oper> <arg> ...] ban.list
日誌進程啓動:
命令行啓動:
sudo -u varnish /home/tools/varnish/bin/varnishncsa -F "%{X-Forwarded-For}i %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\" %D" -a -w /home/tools/varnish/logs/log.log -D
命令行關閉:
pkill varnishncsa
參數:
-a:當把日誌寫到文件裏時,使用附加方式而不是覆蓋;
-C:作正則表達式和字符匹配時不區分大小寫;
-d:在日誌的首部開始處理日誌記錄而不是日誌結尾;
-D:以守護進程方式運行;
-F format:設置輸出日誌格式字符串;
-g :日誌記錄的分組,默認分組爲vxid;
-h:打印varnishlog用法並退出;
-n name:指定varnish實例用來獲取日誌的名稱,若是未指定-n參數,那麼就使用主機名;
-N filename:指定過期VSM實例的文件名。當使用這個參數時,放棄檢查功能會被禁用;
-P file:將進程的PID寫入到指定文件中;
-q query:使用指定的VSL;
-V:打印版本信息並退出;
-w filename:重定向輸出到指定文件,若是-a參數未指定那麼該文件將會被覆蓋重寫
-F 指定格式FORMAT:
指定可使用的日誌格式,若是未指定那麼就使用默認的格式;
默認的日誌格式爲:
%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-agent}i"
支持轉義字符\n和\t;
支持的格式標誌符有:
%b:response的字節大小,不包括HTTP報頭。當沒有字節被髮送時,CLF格式中是 - 而不是0;
%D:處理請求的實際時間,單位爲微秒;
%H:請求協議。若是不知道的話默認爲HTTP/1.0;
%h:遠程主機。若是不知道的話默認爲 - ;
%I:從客戶端接收的總字節大小;
%{X}i:請求報頭X的內容;
%l:遠程登陸用戶(老是爲 - );
%m:請求方法;若是不知道的話默認爲 -
%{X}o:響應報頭X的內容;
%O:發送的總字節大小;
%q:查詢字符串,若是沒有查詢字符串存在,使用空字符串;
%r:請求的第一行。從其餘字段合成,因此可能不會原樣呈現請求;
%s:發送到客戶端的狀態;
%t:請求被接受時的時間(HTTP date/time格式);
%{X}t:請求被接受時的時間(以指定的X格式)。時間規範格式和strftime同樣;
%T:處理請求的實際時間,單位爲秒;
%U:不帶有任何查詢字符串的URL請求。若是不知道的話默認爲 - ;
%u:遠程認證用戶;
%{X}x:擴展變量。支持的變量有:
Varnish:time_firstbyte
從請求開始處理到第一個字節被髮送到客戶端的時間
Varnish:hitmiss
請求是否命中緩存。pipe和pass被看做是未命中的;
Varnish:handling
請求是如何處理的,它是緩存命中 未命中 pass pipe仍是error的;
VCL_Log:key
在VCL中由std.log("key:value")設置值;
腳本啓動:
cat /etc/init.d/varnishncsa
#! /bin/sh # # Control the Varnish Cache NCSA logging daemon # # chkconfig: - 90 10 # description: Varnish Cache logging daemon # processname: varnishncsa # config: # pidfile: /var/run/varnishncsa.pid ### BEGIN INIT INFO # Provides: varnishncsa # Required-Start: $network $local_fs $remote_fs # Required-Stop: $network $local_fs $remote_fs # Default-Start: # Default-Stop: # Short-Description: start and stop varnishncsa # Description: Varnish Cache NCSA logging daemon ### END INIT INFO # Source function library. . /etc/init.d/functions retval=0 pidfile="/home/tools/varnish/varnishncsa.pid" lockfile="/var/lock/subsys/varnishncsa" logfile="/home/tools/varnish/logs/varnishncsa.log" exec="/home/tools/varnish/bin/varnishncsa" prog="varnishncsa" #DAEMON_OPTS="-a -w $logfile -D -P $pidfile" DAEMON_OPTS="-F \"%{X-Forwarded-For}i %l %u %t \'%r\' %s %b \'%{Referer}i\' \'%{User-agent}i\' %D\" -a -w $logfile -D -P $pidfile" # Include varnishncsa defaults [ -e /etc/sysconfig/varnishncsa ] && . /etc/sysconfig/varnishncsa start() { if [ ! -x $exec ] then echo $exec not found exit 5 fi echo -n "Starting varnish ncsa logging daemon: " daemon --pidfile $pidfile sudo -u varnish $exec "$DAEMON_OPTS" echo return $retval } stop() { echo -n "Stopping varnish ncsa logging daemon: " killproc -p $pidfile $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { stop start } reload() { restart } force_reload() { restart } rh_status() { status -p $pidfile $prog } rh_status_q() { rh_status >/dev/null 2>&1 } # See how we were called. case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 restart ;; *) echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
[root@slave2 init.d]# /etc/init.d/varnishncsa start Starting varnish ncsa logging daemon: [ OK ] [root@slave2 init.d]# [root@slave2 init.d]# ps -ef |grep varnishncsa varnish 26502 1 0 16:22 ? 00:00:00 /home/tools/varnish/bin/varnishncsa -F %{X-Forwarded-For}i %l %u %t \'%r\' %s %b \'%{Referer}i\' \'%{User-agent}i\' %D -a -w /home/tools/varnish/logs/varnishncsa.log -D -P /home/tools/varnish/varnishncsa.pid root 26505 3849 0 16:22 pts/0 00:00:00 grep varnishncsa
chmod +x /etc/init.d/varnishncsa
chkconfig varnishncsa on
[root@slave2 logs]# cat varnishncsa.log 192.168.1.223 - - [27/Jun/2019:16:43:26 +0800] 'GET http://192.168.1.224:8888/index.html HTTP/1.1' 200 612 '-' 'curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2' 3008 192.168.1.223 - - [27/Jun/2019:16:43:27 +0800] 'GET http://192.168.1.224:8888/index.html HTTP/1.1' 200 612 '-' 'curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2' 56 192.168.1.223 - - [27/Jun/2019:16:43:31 +0800] 'GET http://192.168.1.224:8888/index.html HTTP/1.1' 200 612 '-' 'curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2' 73 192.168.1.223 - - [27/Jun/2019:16:43:32 +0800] 'GET http://192.168.1.224:8888/index.html HTTP/1.1' 200 612 '-' 'curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2' 56
日誌切割:
cat cutVarnishLog.sh
#!/bin/bash dir=/home/tools/varnish/logs/ date1=`date +%Y%m%d` cd $dir mv varnishncsa.log varnishncsa.${date1}.log /etc/init.d/varnishncsa restart find . -type f -name 'varnishncsa*' -mtime +30 |xargs rm -f
查看內存使用狀況:
[root@slave2 varnish]# ./bin/varnishstat -1|grep SMA.s0.g SMA.s0.g_alloc 2 . Allocations outstanding SMA.s0.g_bytes 916 . Bytes outstanding SMA.s0.g_space 1073740908 . Bytes available
SMA.s0.g_bytes:內存使用量
SMA.s0.g_space:內存剩餘
[root@slave2 logs]# ll /home/tools/varnish/bin/ total 992 -rwxr-xr-x 1 varnish varnish 72572 Jun 26 15:46 varnishadm -rwxr-xr-x 1 varnish varnish 98530 Jun 26 15:46 varnishhist -rwxr-xr-x 1 varnish varnish 96427 Jun 26 15:46 varnishlog -rwxr-xr-x 1 varnish varnish 141592 Jun 26 15:46 varnishncsa -rwxr-xr-x 1 root root 3174 Jun 27 15:06 varnish_reload_vcl -rwxr-xr-x 1 varnish varnish 97192 Jun 26 15:46 varnishstat -rwxr-xr-x 1 varnish varnish 366868 Jun 26 15:46 varnishtest -rwxr-xr-x 1 varnish varnish 124279 Jun 26 15:46 varnishtop
varnishstat 和 varnishtop:統計命令