当前位置:首页 > CN2资讯 > 正文内容

varnish安装配置笔记

4天前CN2资讯

首发于http://www.linux-ch.com/post-12.html

在这个小小的vps上,从一个小小的emlog变成了emlog+ucenter+dz在上面奔跑的场面,是多么的壮观呀~
但是在实际中发现并不是那么的乐观:内存太小,后台的处理能力大打折扣;后台处理能力不高,却全是动态网站;由于是动态网站,所以更无法同时承受更多用户的访问........
在 硬件不会升级的情况下,我尝试了给php装上eaccelerator和memcache,效果并不明显,或者是我没有配置好的原因,nginx在后台依 旧很频繁是调用php-fpm.在这个时候我发现了一些曙光:让数据更少地在后台的进程间传输,然后直接传输给客户端.后在网友殘剑的介绍下尝试了 varnish这个代理软件.

软件下载:wget http://www.varnish-software.com/sites/default/files/varnish-2.1.3.tar.gz

为软件创建用户和存储数据的目录

useradd -M -r -s /sbin/nologin varnish
mkdir /var/vcache

解压,编译,安装

tar -xvf varnish-2.1.3.tar.gz
cd varnish-2.1.3
./configure --prefix=/usr/local/varnish
make
make install



修改配置文件
vim /usr/local/varnish/etc/varnish/default.vcl

 backend linuxch {
     .host = "127.0.0.1";
     .port = "8000";
     .connect_timeout = 1s;
     .first_byte_timeout = 5s;
     .between_bytes_timeout = 2s;
 }
acl purge {
        "127.0.0.1";
}
 sub vcl_recv {
  set req.grace = 30s;
     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") {
         /* Non-RFC2616 or CONNECT which is weird. */
         return (pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
     if (req.http.Authorization || req.http.Cookie) {
        return (pass);
     }
     if (req.http.host ~ "^(.*).linux-ch.com") {
         set req.backend = linuxch;
     }
     else {
        error 404 "Unknown HostName!";
     }
     if (req.request == "PURGE") {
        if (!client.ip ~ purge) {
            error 405 "Not Allowed.";
            return (lookup);
            }
      }
     if (req.url ~ "\.(php)($|\?)") {
        return (pass);
     }
     if (req.http.Cache-Control ~ "(no-cache|max-age=0)") {
        purge_url(req.url);
     }
     return (lookup);
 }

 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 {
     if (req.request == "PURGE") {
        error 404 "Not in cache.";
     }
     return (fetch);
 }
 sub vcl_fetch {
     if (!beresp.cacheable) {
         return (pass);
     }
     if (!(req.url ~ "(admin|login)")) {
         remove beresp.http.Set-Cookie;
     }
     if (req.request == "GET" && req.url ~ "\.(png|swf|txt|png|gif|jpg|jpeg|ico)$") {
        unset req.http.cookie;
        set beresp.ttl = 10d;
     }
     elseif (req.request == "GET" && req.url ~ "\.(html|htm|js|css)$") {
        set beresp.ttl = 3600s;
     else {
         set beresp.ttl = 120s;
      }
     return (deliver);
 }
 sub vcl_deliver {
     if (obj.hits > 0) {
           set resp.http.X-Cache = "HIT";
     } else {
           set resp.http.X-Cache = "MISS";
     }
     return (deliver)
 }
 sub vcl_error {
     set obj.http.Content-Type = "text/html; charset=utf-8";
     synthetic {"
 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html>
   <head>
     <title>"} obj.status " " obj.response {"</title>
   </head>
   <body>
    <h1>Error "} obj.status " " obj.response {"</h1>
    <p>"} obj.response {"</p>
     <h3>Guru Meditation:</h3>
    <p>XID: "} req.xid {"</p>
     <hr>
     <p>Varnish cache server</p>
  </body>
 </html>
 "};
     return (deliver);
 }
 

简单说明下这个配置文件,一开始是定义后端服务器,是在本机上的,端口是8000
然后是管理列表acl,再下面的vcl_recv和 vcl_fetch是用来处理相关请求的.我这里的设置是主机配置"^(.*).linux-ch.com"的交后端,所有php和php?的url也直 接交给后端.在vcl_fetch中定义,所有的png|swf|txt|png|gif|jpg|jpeg|ico文件将在varnish中保留10 天,同时向后端请求的时候会删除没有带有admin|login这些关键字符的以外URL的所有cookie头,这些分别是emlog和dz的登录认证相关的url. html|htm|js|css文件将保留1个小时,其它的保留两分钟.

在2.1后的版本里,原"obj.*"的变量全部变为"beresp.*"了,需要留意一下

然后修改nginx的监听端口至8000,伪静态输出

        rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last;
        rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last;
        rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last;
        rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last;
        rewrite ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2 last;
        break;
 

启动varnishd

/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/etc/varnish/default.vcl -a 74.82.172.143:80 -u varnish -g varnish -s file,/var/vcache/varnish_storage.bin,64M

为方便管理,利用官方的一个脚本,将varnish添加到系统服务里,并随机启动
vim /etc/rc.d/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 $?


给脚本执行权限,添加服务,修改启动等级
 

chmod +x /etc/rc.d/init.d/varnish
/sbin/chkconfig --add varnish
/sbin/chkconfig --level 345 varnish on

varnish的配置调用文件.是用来告诉程序从哪里读取配置文件,启动参数有哪些等
vim /usr/local/varnish/etc/varnish/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

## Alternative 2, Configuration with VCL
DAEMON_OPTS="-a 74.82.172.143:80 \
             -f /usr/local/varnish/etc/varnish/default.vcl \
             -T localhost:6082 \
             -u varnish -g varnish \
             -n /var/vcache \
             -s file,/var/vcache/varnish_storage.bin,64M"

到此,整个配置就已经弄好了,整个网站的数据拓扑如下:

client
  ↓↑ ↑
varnish
  ↓   ↑
nginx
  ↓   ↑
php-fpm

如果varnish上面有缓存,将直接传递给用户端,没有的再向后方要,再传递给用户,同时符合存储条件的将保留下来,直至过期.从测试的效果来看,还是比较理想的,实际情况,还得看观察一段时间~
 

 

 

 

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/20333.html

    分享给朋友:

    “varnish安装配置笔记” 的相关文章

    inet.ws VPS测评:揭示高性价比主机服务的真实体验与分析

    在如今这个互联网发展的时代,选择一个可靠的虚拟专用服务器(VPS)提供商至关重要。我们要介绍的就是 inet.ws,一家国外的主机服务商。inet.ws 的主营业务是销售全球多节点的 VPS 服务器。自从 2023 年 8 月推出了全场 13 个机房的 7.5 折优惠活动后,它的性价比愈发吸引了许多...

    Hostodo VPS主机使用体验与性能评测

    当我第一次听说Hostodo时,正是2014年,这家美国VPS主机商在市场上开始崭露头角。印象中,它的低价VPS产品让我感到十分吸引,尤其是在对比市场上其他的主机商时,Hostodo的性价比确实相当有优势。它主营的KVM型和NVMe硬盘的KVM型VPS在当时的市场中并不是常见的选择,迅速吸引了许多站...

    HostYun:高性价比VPS服务的理想选择

    HostYun,最早被称作主机分享,成立于2008年,专注于提供性价比极高的VPS服务。在众多IDC品牌中,HostYun凭借其低价策略迅速占领了一席之地。作为一个以KVM和XEN虚拟化技术为基础的平台,HostYun不仅满足了用户对低成本服务的需求,也为学习、测试和小型项目的部署提供了理想的选择。...

    如何选择合适的IP站及其运作原理

    IP站的运作原理 在探讨IP站的运作原理之前,首先需要明确什么是IP站。简单来说,IP站是一种特殊的网络服务,它利用互联网协议(IP)提供不同的网络功能和服务。每个IP站都与一个或多个IP地址相连,能够用来访问信息、数据或应用程序。在我的网络体验中,无论是个人使用还是企业应用,IP站总是扮演着至关重...

    深入了解DMIT不同线路,优化您的网络体验

    在开始深入了解DMIT这一知名VPS提供商之前,我想先分享一下我对于它的初步印象。DMIT的使命是为用户提供高性能、稳定的VPS解决方案,特别是在跨境访问方面表现不俗。他们采用的CN2优化线路更是让其在众多竞争对手中脱颖而出。通过不断的发展与创新,DMIT为不同需求的用户提供了多种线路选择。 DMI...

    VPS是什么?全面解析虚拟专用服务器的优势与选择

    VPS 是 什么 VPS,全称虚拟专用服务器,是一种通过虚拟化技术在物理服务器上创建多个独立环境的方案。具体来说,每个VPS都能运行自己的操作系统,并拥有独立的CPU、内存和存储资源。这就意味着,用户可以像在独立服务器上那样自由管理自己的VPS,进行各种应用和服务的部署。 最初,当我接触到VPS时,...