基于Varnish 3.0.4的网站静态加速(3)

7、Varnish 配置重载脚本

# vim /usr/local/varnish/sbin/varnish_reload_vcl

#!/bin/bash

#

# reload vcl revisited

# A script that loads new vcl based on data from /usr/local/varnish/etc/varnish.conf

# 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\"

"

}

# Read configuration

. /usr/local/varnish/etc/varnish.conf

$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"

echo

fi

# Done parsing, set up command

#VARNISHADM="varnishadm $secret -T $VARNISH_ADMIN_LISTEN_ADDRESS:$VARNISH_ADMIN_LISTEN_PORT"

VARNISHADM="/usr/local/varnish/bin/varnishadm -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 $3 } ' | 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 $3 } ' )

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

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


-----------------------------------------------------------------------------------------------------------------------------------------------


8、Varnish启动脚本

# vim /etc/init.d/varnishd

#!/bin/bash

#

# varnish Control the Varnish Cache

#

# chkconfig: - 90 10

# description: Varnish is a high-perfomance HTTP accelerator

# processname: varnishd

# config: /usr/local/varnish/etc/varnish.conf

# PIDFILE: /var/run/varnishd.pid

#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

BINFILE="/usr/local/varnish/sbin/varnishd"

PROG="varnishd"

RETVAL=0

GFILE="/usr/local/varnish/etc/varnish.conf"

PIDFILE="/var/run/varnish.pid"

LOCKFILE="/var/lock/varnish.lock"

RELOAD_EXEC="/usr/local/varnish/sbin/varnish_reload_vcl"

[[ -e ${GFILE} ]] && . ${GFILE}

start() {

IS_EXIST=`ps -A -oppid,pid,cmd | grep sbin/${PROG} | grep -v grep`

[[ -n "${IS_EXIST}" ]] && echo "The process of ${PROG} has been running." && exit 1

[[ ! -x ${BINFILE} ]] && echo ${BINFILE} has no found && exit 5

[[ ! -f ${GFILE} ]] && echo ${GFILE} has no found && exit 6

echo -n "Starting Varnish Cache......"

ulimit -n ${NFILES:-131072}

ulimit -l ${MEMLOCK:-82000}

ulimit -u ${NPROCS:-unlimited}

if [[ "${DAEMON_OPTS}X" == "X" ]]; then

echo -n "Please setting DAEMON_OPTS options in ${GFILE}"

RETVAL=6

else

VARNISH_CACHE_DIR=`dirname $VARNISH_STORAGE_FILE`

RETVAL=`grep -w '^VARNISH_STORAGE' ${GFILE} | grep malloc`

if [[ "${RETVAL}X" = "X" ]]; then

mkdir -p ${VARNISH_CACHE_DIR} && chown -R nobody:nogroup ${VARNISH_CACHE_DIR}

else

[[ -e ${VARNISH_CACHE_DIR} ]] && rm -rf ${VARNISH_CACHE_DIR}

fi

${BINFILE} -P ${PIDFILE} $DAEMON_OPTS >/dev/null 2>&1

RETVAL=$?

echo

[[ $RETVAL -eq 0 ]] && touch ${LOCKFILE}

fi

return $RETVAL

}

stop() {

echo -n "Stopping Varnish Cache......"

/sbin/killproc -QUIT ${PROG}

RETVAL=$?

echo

[[ $RETVAL -eq 0 ]] && rm -f ${LOCKFILE}

return $RETVAL

}

restart() {

stop

sleep 1

start

}

reload() {

[[ "$RELOAD_VCL" = "1" ]] && $RELOAD_EXEC || restart

RETVAL=$?

echo

return $RETVAL

}

configtest() {

if [[ -f ${GFILE} ]]; then

${BINFILE} -f ${GFILE} -C -n /tmp >/dev/null 2>&1

RETVAL=$?

[[ $? -eq 0 ]] && echo "The syntax is ok." || echo "The syntax is error."

else

RETVAL=$?

echo "The config file: ${GFILE} is no exists."

fi

return $RETVAL

}

case "$1" in

start|stop|restart|reload|configtest)

$1

;;

*)

echo "Usage: $0 {start|stop|restart|reload|configtest}"

exit 2

esac

exit $RETVAL


# chmod +x /etc/init.d/varnishd


# chkconfig --add varnishd

-----------------------------------------------------------------------------------------------------------------------------------------------

9、Varnish服务健康检查脚本

# vim /usr/local/varnish/sbin/check_varnish_health.sh

#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin

## varnish启动参数配置文件

VARNISH_CONFIG="/usr/local/varnish/etc/varnish.conf"

IS_DEBUG=false

[[ -e ${VARNISH_CONFIG} ]] && . ${VARNISH_CONFIG}

${IS_DEBUG} && echo "监听地址:${VARNISH_LISTEN_ADDRESS}  监听端口:${VARNISH_LISTEN_PORT}"

LAN_IPADDR=`/sbin/ifconfig eth1 | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`

RETVAL=`nmap --system-dns -sT -p ${VARNISH_LISTEN_PORT} ${LAN_IPADDR} | grep open`

[[ -z ${RETVAL} ]] && /sbin/service varnishd restart >/dev/null 2>&1


# crontab -e

*/5 * * * * /usr/local/varnish/sbin/check_varnish_health.sh >/dev/null 2>&1

Varnish 的详细介绍请点这里
Varnish 的下载地址请点这里

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/77c1658830e101f236ca1ac80968031e.html