热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Varnish服务器的安装配置过程

安装#yuminstallautomakeautoconflibtoolncurses-devellibxsltgroffpcre-develpkgconfig-y#wgethttp://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz#tar-zxvfvarnish
安装
# yum install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig -y

# wget http://repo.varnish-cache.org/source/varnish-3.0.2.tar.gz
# tar -zxvf varnish-3.0.2.tar.gz
# cd varnish-3.0.2
# sh autogen.sh
# ./configure --prefix=/usr/local/varnish-3.0.2
# make
# make check
# make install
# ln -s /usr/local/varnish-3.0.2 /usr/local/varnish

# vi /etc/profile
增加:
export PATH=$PATH:/usr/local/varnish/bin

安装完成。

设置工作目录
# groupadd varnish
# useradd -g varnish -d /usr/local/varnish/var/varnish -s /sbin/nologin varnish

# passwd -l varnish

# mkdir /home/varnish
# chown -R varnish:varnish /home/varnish
# mkdir -p /home/log/varnish
# chown -R varnish /home/log/varnish

配置文件

# vi /usr/local/varnish/etc/varnish/varnish.vcl
director letaowwwserver random {
   { .backend = { .host = "192.168.100.108"; .port = "http"; .probe = { .url = "/heartbeat.aspx"; .timeout = 500 ms; .interval = 1s; .window = 5; .threshold = 3; }} .weight = 1; }
   { .backend = { .host = "192.168.100.104"; .port = "http"; .probe = { .url = "/heartbeat.aspx"; .timeout = 500 ms; .interval = 1s; .window = 5; .threshold = 3; }} .weight = 1; }
   { .backend = { .host = "192.168.100.105"; .port = "http"; .probe = { .url = "/heartbeat.aspx"; .timeout = 500 ms; .interval = 1s; .window = 5; .threshold = 3; }} .weight = 1; }
   { .backend = { .host = "192.168.100.109"; .port = "http"; .probe = { .url = "/heartbeat.aspx"; .timeout = 500 ms; .interval = 1s; .window = 5; .threshold = 3; }} .weight = 1; }
   { .backend = { .host = "192.168.100.110"; .port = "http"; .probe = { .url = "/heartbeat.aspx"; .timeout = 500 ms; .interval = 1s; .window = 5; .threshold = 3; }} .weight = 1; }
}

backend letaotestserver {
       .host = "192.168.100.119";
       .port = "80";
}

sub vcl_recv {
        if (req.request != "GET" && req.request != "HEAD") {
                error 405 req.url;
        }
if (req.http.Accept-Encoding) {
if (req.url ~"(?i)\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ipa|sisx|apk)$") {
   # No point in compressing these
   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";
} else {
   # unkown algorithm
   remove req.http.Accept-Encoding;
}
}
set req.http.host = "www.letao.com";

     if (req.url ~"(?i).*\/letaozu\/.*\.(css|jpg|jpeg|gif|png|js|swf|apk|ipa|sisx)" || req.url ~"(?i).*\/images\/.*\.(css|jpg|jpeg|gif|png|js|swf|apk|ipa|sisx)" ) {
       set req.backend = letaotestserver;
       remove req.http.COOKIE;
       return(lookup);
} elsif (req.url ~"(?i)\.(css|jpg|jpeg|gif|png|js|swf|apk|ipa|sisx)") {
               set req.backend = letaowwwserver;
               remove req.http.COOKIE;
 return(lookup);
}
else {
               error 405 req.url;
}
}

sub vcl_fetch{

  if(beresp.status >= 400 && beresp.status <= 500)
  {
    set beresp.ttl = 900s;
  }
  else if(beresp.ttl > 0s){
    remove beresp.http.set-COOKIE;
unset beresp.http.Server;
unset beresp.http.X-Powered-By;
        set beresp.http.cache-cOntrol= "max-age = 86400000";
    set beresp.ttl = 100d;
set beresp.http.Expires = "Thu, 23 Jan 2020 17:18:52 GMT";
        set beresp.http.magicmarker = "2";
  }
}

sub vcl_deliver{
    if(resp.http.magicmarker){ 
        set resp.http.age = "0";
        unset resp.http.Etag;
unset resp.http.Varnish_Server;
        unset resp.http.X-Varnish;
        unset resp.http.Via;
     }
}

sub vcl_hit {
       if (req.request == "PURGE") {
               set obj.ttl = 0s;
               error 200 "Purged.";
       }
}

sub vcl_miss {
       if (req.request == "PURGE") {
               error 404 "Not in cache.";
       }
}

优化linux内核:
# vi /etc/sysctl.conf 
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_synCOOKIEs = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000

# sysctl -p

启动varnish测试:
/usr/local/varnish/sbin/varnishd -P /var/run/varnish.pid -a :80 -T 127.0.0.1:6082 -n /home/varnish -f /usr/local/varnish/etc/varnish/varnish.vcl -u varnish -g varnish -s file,/home/varnish/varnish_storage.bin,10G -t 8640000 -p lru_interval=20 -h classic,500009 -p thread_pools=4

记录日志:
/usr/local/varnish/bin/varnishncsa -n /home/varnish -w /home/log/varnish/varnish.log &

查看统计:
varnishstat -n /home/varnish

开放防火墙:
# vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

清除缓存:
/usr/local/varnish/bin/varnishadm  -T 0:6082 ban.url /test.html$

设置启动脚本:

# cp /root/varnish-3.0.2/redhat/varnish_reload_vcl /usr/local/varnish/bin/
# vi /usr/local/varnish/bin/varnish_reload_vcl
在. /etc/sysconfig/varnish下面增加:
. /etc/profile

然后
# cp /root/varnish-3.0.2/redhat/varnish.sysconfig /etc/sysconfig/varnish

# vi /root/varnish-3.0.2/redhat/varnish.initrc
#! /bin/sh
#
# varnish Control the Varnish Cache
#
# chkconfig: - 90 10
# description: Varnish is a high-perfomance HTTP accelerator
# processname: varnishd
# config: /etc/sysconfig/varnish
# pidfile: /var/run/varnishd.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-perfomance HTTP accelerator
### END INIT INFO

# Source function library.
. /etc/init.d/functions

retval=0
pidfile=/var/run/varnish.pid

#exec="/usr/sbin/varnishd"
exec="/usr/local/varnish/sbin/varnishd"
#reload_exec="/usr/bin/varnish_reload_vcl"
reload_exec="/usr/local/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}

        # $DAEMON_OPTS is set in /etc/sysconfig/varnish. At least, one
        # has to set up a backend, or /tmp will be used, which is a bad idea.
        if [ "$DAEMON_OPTS" = "" ]; then
                echo "\$DAEMON_OPTS empty."
                echo -n "Please put configuration options in $config"
                return 6
        else
                # Varnish always gives output on STDOUT
                daemon --pidfile $pidfile  $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
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 -f "$VARNISH_VCL_CONF" -C -n /tmp > /dev/null && echo "Syntax ok"
    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 $?

# cp /root/varnish-3.0.2/redhat/varnish.initrc /etc/init.d/varnish
# chkconfig --add varnish
# chmod +x /etc/init.d/varnish
# chkconfig varnish on

设置启动参数配置文件
# vi /etc/sysconfig/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=82000

# Maximum size of corefile (for ulimit -c). Default in Fedora is 0
# DAEMON_COREFILE_LIMIT="unlimited"

# Set this to 1 to make init script reload try to 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, or in short,
# use Alternative 3, Advanced configuration, below
RELOAD_VCL=1

# This file contains 4 alternatives, please use only one.

## Alternative 1, Minimal configuration, no VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# content server on localhost:8080.  Use a fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -b localhost:8080 \
#             -u varnish -g varnish \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"

## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request.  Use a
# fixed-size cache file.
#
#DAEMON_OPTS="-a :6081 \
#             -T localhost:6082 \
#             -f /etc/varnish/default.vcl \
#             -u varnish -g varnish \
#             -S /etc/varnish/secret \
#             -s file,/var/lib/varnish/varnish_storage.bin,1G"

## Alternative 3, Advanced configuration
#
# See varnishd(1) for more information.
#
# # Main configuration file. You probably want to change it :)
#VARNISH_VCL_COnF=/etc/varnish/default.vcl
VARNISH_VCL_COnF=/usr/local/varnish/etc/varnish/varnish.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_ADDRESS=
#VARNISH_LISTEN_PORT=6081
VARNISH_LISTEN_PORT=80
#
# # Telnet admin interface listen address and port
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
#
# # Shared secret file for admin interface
#VARNISH_SECRET_FILE=/etc/varnish/secret
#
# # The minimum number of worker threads to start
#VARNISH_MIN_THREADS=1
#
# # The Maximum number of worker threads to start
#VARNISH_MAX_THREADS=1000
#
# # Idle timeout for worker threads
#VARNISH_THREAD_TIMEOUT=120
#
# # Cache file location
#VARNISH_STORAGE_FILE=/var/lib/varnish/varnish_storage.bin
VARNISH_STORAGE_FILE=/home/varnish/varnish_storage.bin
#
# # Cache file size: in bytes, optionally using k / M / G / T suffix,
# # or in percentage of available disk space using the % suffix.
#VARNISH_STORAGE_SIZE=1G
VARNISH_STORAGE_SIZE=10G
#
# # Backend storage specification
VARNISH_STORAGE="file,${VARNISH_STORAGE_FILE},${VARNISH_STORAGE_SIZE}"
#
# # Default TTL used when the backend does not specify one
#VARNISH_TTL=120
VARNISH_TTL=8640000
#
# # DAEMON_OPTS is used by the init script.  If you add or remove options, make
# # sure you update this section, too.
DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \
             -f ${VARNISH_VCL_CONF} \
             -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \
             -t ${VARNISH_TTL} \
             -u varnish -g varnish \
             -s ${VARNISH_STORAGE} -n /home/varnish -p lru_interval=20 -h classic,500009 -p thread_pools=4"

#             -S ${VARNISH_SECRET_FILE} \
#             -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \
#

## Alternative 4, Do It Yourself. See varnishd(1) for more information.
#
# DAEMON_OPTS=""


推荐阅读
  • 在1995年,Simon Plouffe 发现了一种特殊的求和方法来表示某些常数。两年后,Bailey 和 Borwein 在他们的论文中发表了这一发现,这种方法被命名为 Bailey-Borwein-Plouffe (BBP) 公式。该问题要求计算圆周率 π 的第 n 个十六进制数字。 ... [详细]
  • 本文探讨了如何通过优化 DOM 操作来提升 JavaScript 的性能,包括使用 `createElement` 函数、动画元素、理解重绘事件及处理鼠标滚动事件等关键主题。 ... [详细]
  • 本文介绍了SIP(Session Initiation Protocol,会话发起协议)的基本概念、功能、消息格式及其实现机制。SIP是一种在IP网络上用于建立、管理和终止多媒体通信会话的应用层协议。 ... [详细]
  • 二维码的实现与应用
    本文介绍了二维码的基本概念、分类及其优缺点,并详细描述了如何使用Java编程语言结合第三方库(如ZXing和qrcode.jar)来实现二维码的生成与解析。 ... [详细]
  • publicclassBindActionextendsActionSupport{privateStringproString;privateStringcitString; ... [详细]
  • 本文详细探讨了Linux系统中的文件权限设置,包括常见的755、700等权限模式,以及这些权限在实际应用中的具体含义和作用。 ... [详细]
  • 深入探讨前端代码优化策略
    本文深入讨论了前端开发中代码优化的关键技术,包括JavaScript、HTML和CSS的优化方法,旨在提升网页加载速度和用户体验。 ... [详细]
  • 解决JavaScript中法语字符排序问题
    在开发一个使用JavaScript、HTML和CSS的Web应用时,遇到从SQLite数据库中提取的法语词汇排序不正确的问题,特别是带重音符号的字母未按预期排序。 ... [详细]
  • 理解浏览器历史记录(2)hashchange、pushState
    阅读目录1.hashchange2.pushState本文也是一篇基础文章。继上文之后,本打算去研究pushState,偶然在一些信息中发现了锚点变 ... [详细]
  • JUnit下的测试和suite
    nsitionalENhttp:www.w3.orgTRxhtml1DTDxhtml1-transitional.dtd ... [详细]
  • 数据类型--char一、char1.1char占用2个字节char取值范围:【0~65535】char采用unicode编码方式char类型的字面量用单引号括起来char可以存储一 ... [详细]
  • 本文详细介绍了iOS应用的生命周期,包括各个状态及其转换过程中的关键方法调用。 ... [详细]
  • 项目风险管理策略与实践
    本文探讨了项目风险管理的关键环节,包括风险管理规划、风险识别、风险分析(定性和定量)、风险应对策略规划及风险控制。旨在通过系统的方法提升项目成功率,减少不确定因素对项目的影响。 ... [详细]
  • 探索AI智能机器人自动盈利系统的构建
    用户可通过支付198元押金及30元设备维护费租赁AI智能机器人,推荐他人加入可获得相应佣金。随着推荐人数的增加,用户将逐步解锁更高版本,享受更多收益。 ... [详细]
  • Windows操作系统提供了Encrypting File System (EFS)作为内置的数据加密工具,特别适用于对NTFS分区上的文件和文件夹进行加密处理。本文将详细介绍如何使用EFS加密文件夹,以及加密过程中的注意事项。 ... [详细]
author-avatar
李伯翔刚瑋嘉军
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有