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

CentOS5.5环境下安装配置varnish

#!/bin/bash#BYkerryhu#MAIL:king_819@163.com#BLOG:http://kerry.blog.51cto.com#PleasemanualoperationyumofbeforeOperation.....#更新系统时间yuminstall-yntpntpdatetim
#!/bin/bash
# BY server110hu
# MAIL:king_819@163.com
# BLOG:http://server110.blog.51cto.com
# Please manual operation yum of before Operation.....
#============================ 更新系统时间 ============================
yum install -y ntp
ntpdate time.nist.gov
echo "00 01 * * * ntpdate time.nist.gov" >> /etc/crontab
#============================ 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/server110.vcl
backend server110 {              #定义后端服务器名
 .host = "192.168.9.203";    #定义后端服务器IP
 .port = "80";      #定义后端服务器端口
}
backend king {
 .host = "192.168.9.204";
 .port = "80";
}
#定义访问控制列表,充许那些IP清除varnish 缓存
acl local {
 "localhost";
 "127.0.0.1";
}
#判断host请求针对那个后端服务器
sub vcl_recv {
 if (req.http.host ~ "^(www.)?server110.com$") {  #泛域名的写法"^(.*.)?server110.com$"
  set req.backend = server110;
 }
 elsif (req.http.host ~ "^(www.)?king.com$") {
  set req.backend = king;
 }
 else {
  error 404 "Unknown HostName!"; #如果都不匹配,返回404错误
 }
 #不充许非访问控制列表的IP进行varnish缓存清除
 if(req.request == "PURGE") {
  if (!client.ip ~ local) {
   error 405 "Not Allowed.";
   return (lookup);
   }
 }
 #清除url中有jpg|png|gif等文件的COOKIE
 if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$") {
  unset req.http.COOKIE;
 }
 #取消服务器上images目录下所有文件的COOKIE
 if (req.url ~ "^/images") {
  unset req.http.COOKIE;
 }
 #判断req.http.X-Forwarded-For,如果前端有多重反向代理,这样可以获取客户端IP地址。
 if (req.http.x-forwarded-for) {
  set req.http.X-Forwarded-For =
  req.http.X-Forwarded-For ", " client.ip;
 }
 else {
  set req.http.X-Forwarded-For = client.ip;
 }
 if (req.request != "GET" &&
     req.request != "HEAD" &&
     req.request != "PUT" &&
     req.request != "POST" &&
     req.request != "TRACE" &&
     req.request != "OPTIONS" &&
     req.request != "DELETE") {
  return (pipe);
 }
 #针对请求和url地址判断,是否在varnish缓存里查找
 if (req.request != "GET" && req.request != "HEAD") {
  return (pass);
 } ## 对非GET|HEAD请求的直接转发给后端服务器
 if (req.http.Authorization || req.http.COOKIE) {
  return (pass);
 }
 if (req.request == "GET" && req.url ~ "\.(php)($|\?)") {
  return (pass);
  } #对GET请求,且url里以.php和.php?结尾的,直接转发给后端服务器
      return (lookup);
 }  #除了以上的访问以外,都在varnish缓存里查找
sub vcl_pipe {
 return (pipe);
}
sub vcl_pass {
 return (pass);
}
sub vcl_hash {
 set req.hash += req.url;
 if (req.http.host) {
  set req.hash += req.http.host;
 } else {
  set req.hash += server.ip;
 }
 return (hash);
}
sub vcl_hit {
 if (!obj.cacheable) {
  return (pass);
 }
 if (req.request == "PURGE") {
         set obj.ttl = 0s;
         error 200 "Purged.";
      }
 return (deliver);
}
sub vcl_miss {
 return (fetch);
}
sub vcl_fetch {
 if (!beresp.cacheable) {
  return (pass);
 }
 if (beresp.http.Set-COOKIE) {
  return (pass);
 }
 #WEB服务器指明不缓存的内容,varnish服务器不缓存
 if (beresp.http.Pragma ~ "no-cache" ||
     beresp.http.Cache-Control ~ "no-cache" ||
beresp.http.Cache-Control ~ "private") {
  return (pass);
      }
      #对.txt .js .shtml结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为10天
 if (req.request == "GET" && req.url ~ "\.(txt|js|css|shtml|html|htm)$") {
set beresp.ttl = 3600s;
 }
 else {
set beresp.ttl = 10d;
 }
 return (deliver);
}
#添加在页面head头信息中查看缓存命中情况
sub vcl_deliver {
 set resp.http.x-hits = obj.hits ;
 if (obj.hits > 0) {
  set resp.http.X-Cache = "HIT cqtel-bbs";
 }
 else {
      set resp.http.X-Cache = "MISS cqtel-bbs";
 }
}
sub vcl_error {
 set obj.http.Content-Type = "text/html; charset=utf-8";
 synthetic {"



  
     
  
  
      Error "} obj.status " " obj.response {"
     

"} obj.response {"


     

Guru Meditation:


     

XID: "} req.xid {"


     

     

         bbs cache server
     

  

"};
 return (deliver);
}
注意:在2.1后的版本里,原"obj.*"的变量全部变为"beresp.*"了,需要留意一下 启动varnish
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/server110.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/server110.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
cat /etc/rc.local
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -u www -g www -f /usr/local/varnish/etc/varnish/server110.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.server110.com$ && req.url ~ /static/image/tp.php"
说明:
192.168.9.201:3000 为被清除缓存服务器地址
www.server110.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

#============================ 内核优化 ==============================
vi /etc/sysctl.conf
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 = 3276800
/sbin/sysctl -p
#===================== Varnish添加到服务自启动 ======================
配置启动文件
vi /etc/init.d/varnish
#! /bin/sh
#
# varnish Control the varnish HTTP accelerator
#
# chkconfig: - 90 10
# description: Varnish is a high-perfomance HTTP accelerator
# processname: varnishd
# config: /etc/sysconfig/varnish
# pidfile: /var/run/varnish/varnishd.pid
### BEGIN INIT INFO
# Provides: varnish
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# 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/local/varnish/sbin/varnishd"
prog="varnishd"
cOnfig="/usr/local/varnish/etc/varnish/varnish"
lockfile="/var/lock/subsys/varnish"
# Include varnish defaults
[ -e /usr/local/varnish/etc/varnish/varnish ] && . /usr/local/varnish/etc/varnish/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 HTTP accelerator: "
    # 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   $exec -P $pidfile "$DAEMON_OPTS" > /dev/null 2>&1
        retval=$?
        if [ $retval -eq 0 ]
        then
touch $lockfile
echo_success
echo
        else
echo_failure
        fi
        return $retval
    fi
}
stop() {
    echo -n "Stopping varnish HTTP accelerator: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    stop
    start
}
reload() {
    restart
}
force_reload() {
    restart
}
rh_status() {
    status $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 $?
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/server110.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
/etc/init.d/varnish stop

推荐阅读
  • Python自动化测试入门:Selenium环境搭建
    本文详细介绍如何在Python环境中安装和配置Selenium,包括开发工具PyCharm的安装、Python环境的设置以及Selenium包的安装方法。此外,还提供了编写和运行第一个自动化测试脚本的步骤。 ... [详细]
  • 访问一个网页的全过程
    准备:DHCPUDPIP和以太网启动主机,用一根以太网电缆连接到学校的以太网交换机,交换机又与学校的路由器相连.学校的这台路由器与一个ISP链接,此ISP(Intern ... [详细]
  • CentOS 系统管理基础
    本文介绍了如何在 CentOS 中查询系统版本、内核版本、位数以及磁盘分区的相关知识。通过这些命令,用户可以快速了解系统的配置和磁盘结构。 ... [详细]
  • 本文深入探讨了 Exchange Server 2010 中客户端访问的代理和重定向机制,特别是在跨站点环境中如何配置这些功能以确保用户能够顺利访问邮箱服务。通过详细解析不同场景下的应用,帮助管理员更好地理解和实施相关设置。 ... [详细]
  • 本文旨在回顾并总结近期学习的.NET Core基础知识,通过具体的操作指南加深理解,并为初学者提供实用建议,避免常见的错误和陷阱。内容涵盖CentOS的安装配置、.NET Core环境搭建及网站部署等。 ... [详细]
  • 深入理解动态链接库及其应用
    本文将探讨动态链接库的基本概念,包括Windows下的动态链接库(DLL)和Linux下的共享库(Shared Library),并详细介绍如何在Linux环境中创建和使用这些库。 ... [详细]
  • CentOS 7.2 配置防火墙端口开放
    本文介绍如何在 CentOS 7.2 系统上配置防火墙以开放特定的服务端口,包括 FTP 服务的临时与永久开放方法,以及如何验证配置是否生效。 ... [详细]
  • 在Linux系统上构建Web服务器的详细步骤
    本文详细介绍了如何在Linux系统上搭建Web服务器的过程,包括安装Apache、PHP和MySQL等关键组件,以及遇到的一些常见问题及其解决方案。 ... [详细]
  • 本文详细介绍了 phpMyAdmin 的安装与配置方法,适用于多个版本的 phpMyAdmin。通过本教程,您将掌握从下载到部署的完整流程,并了解如何根据不同的环境进行必要的配置调整。 ... [详细]
  • 针对表格详情切换功能,对HTML代码进行了优化和调整,以确保更好的显示效果和用户体验。 ... [详细]
  • 本文探讨了如何在Classic ASP中实现与PHP的hash_hmac('SHA256', $message, pack('H*', $secret))函数等效的哈希生成方法。通过分析不同实现方式及其产生的差异,提供了一种使用Microsoft .NET Framework的解决方案。 ... [详细]
  • 使用JS、HTML5和C3创建自定义弹出窗口
    本文介绍如何结合JavaScript、HTML5和C3.js来实现一个功能丰富的自定义弹出窗口。通过具体的代码示例,详细讲解了实现过程中的关键步骤和技术要点。 ... [详细]
  • ArcXML:互联网空间数据交换的专用语言
    ArcXML是一种专为ArcIMS平台设计的数据交换协议,基于XML标准,用于在不同组件之间传输和描述地理空间数据。本文将详细介绍ArcXML的背景、用途及其与XML的关系。 ... [详细]
  • 远程过程调用(RPC)是一种允许客户端通过网络请求服务器执行特定功能的技术。它简化了分布式系统的交互,使开发者可以像调用本地函数一样调用远程服务,并获得返回结果。本文将深入探讨RPC的工作原理、发展历程及其在现代技术中的应用。 ... [详细]
  • CentOS 7.6环境下Prometheus与Grafana的集成部署指南
    本文旨在提供一套详细的步骤,指导读者如何在CentOS 7.6操作系统上成功安装和配置Prometheus 2.17.1及Grafana 6.7.2-1,实现高效的数据监控与可视化。 ... [详细]
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社区 版权所有