一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

linux中编译安装Varnish3.03及配置

时间:2013-12-10 编辑:简简单单 来源:一聚教程网


这是关于Varnish3.03编译安装配置的笔记

安装

 代码如下 复制代码

yum -y install gcc gcc-c++ file bison patch unzip mlocate flex wget diffutils automake autoconf kernel-devel gd cpp readline-devel openssl openssl-devel vim-minimal nano libjpeg libjpeg       -devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel        e2fsprogs e2fsprogs-devel libidn libidn-devel openldap openldap-devel openldap-clients openldap-servers nss_ldap gettext gettext-devel expat-devel libcap libcap-devel libtool libtool-     ltdl-devel pam-devel pcre-devel ncurses-devel subversion bind-utils rsync libxslt groff pkgconfig

    # compile Varnish-Cache
    cd /usr/local/src
    wget -c http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz
    tar -zxf varnish-3.0.3.tar.gz
    cd varnish*
    ./autogen.sh
    ./configure --prefix=/usr/local/varnish
    make
    make install

    /usr/sbin/groupadd -g 1000 -o -r varnish
    /usr/sbin/useradd -M -g varnish -o -r -d /dev/zero -s /bin/false -c "Varnish-Cache Server" -u 1000 varnish
    mkdir -p {/etc/varnish,/var/log/varnish,/var/lib/varnish}
    /usr/bin/uuidgen > /etc/varnish/secret && chmod 0600 /etc/varnish/secret
    /usr/bin/install -m 755 ./redhat/varnish.initrc /etc/rc.d/init.d/varnish
    /usr/bin/install -m 755 ./redhat/varnishncsa.initrc /etc/rc.d/init.d/varnishncsa
    /usr/bin/install -m 644 ./redhat/varnish.sysconfig /etc/sysconfig/varnish
    /usr/bin/install -m 755 ./redhat/varnish_reload_vcl /usr/local/varnish/bin/

    ln -s /usr/local/varnish/bin/varnish_reload_vcl /usr/bin/
    ln -s /usr/local/varnish/bin/varnishncsa /usr/bin/
    ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/
    ln -s /usr/local/varnish/bin/varnishadm /usr/bin/
    ln -s /usr/local/varnish/bin/varnishstat /usr/bin/
    ln -s /usr/local/varnish/bin/varnishhist /usr/bin/
    ln -s /usr/local/varnish/bin/varnishlog /usr/bin/
    ln -s /usr/local/varnish/bin/varnishtop /usr/bin/

    chkconfig --add varnish
    chkconfig varnish on
    chkconfig --add varnishncsa
    chkconfig varnishncsa on

配置

使用下面的代码来替换/etc/sysconfig/varnish中的内容,注意将下面代码中的121.199.13.169替换为你的vps的公网ip

 代码如下 复制代码

 

    # 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 number of threads (for ulimit -u)
    NPROCS="unlimited"

    # 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
    #
    # # 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=121.199.13.169
    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=50
    #
    # # 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=/dev/shm/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
    #
    # # 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
    #
    # # 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}
                -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT}
                -u varnish -g varnish
                -S ${VARNISH_SECRET_FILE}
                -s ${VARNISH_STORAGE}"
    #

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


使用下面的代码来替换/etc/varnish/default.vcl中的内容,如果/etc/varnish/default.vcl文件不存在,那就创建吧

 代码如下 复制代码

 

    # This is a basic VCL configuration file for varnish.  See the vcl(7)
    # man page for details on VCL syntax and semantics.
    #
    # Default backend definition.  Set this to point to your content
    # server.
    #
    backend default {
    .host = "127.0.0.1";
    .port = "80";
    }sub vcl_recv {
    remove req.http.X-real-ip;
    set req.http.X-real-ip = client.ip;
    set req.http.X-Forwarded-For = client.ip;

    if (req.http.Authorization) {
    return (pass);
    }

    if(req.url ~ ".(php)$" ){
    return (pass);
    }

    if (req.request != "GET" &&
    req.request != "HEAD" &&
    req.request != "PUT" &&
    req.request != "POST" &&
    req.request != "TRACE" &&
    req.request != "OPTIONS" &&
    req.request != "DELETE")
    { return (pipe); }

    if (req.request != "GET" && req.request != "HEAD") {
    return (pass);
    }

    if (req.request == "GET" && req.url ~ "(?i).(js|css|jpg|jpeg|png|gif|ico|rar|gz|bz2|mp3|swf|flv)$") {
    unset req.http.cookie;
    return (lookup);
    }
    }

    sub vcl_fetch {
    if (req.request == "GET" && req.url ~ "(?i).(js|css|jpg|jpeg|png|gif|ico|rar|gz|bz2|mp3|swf|flv)$") {
    set beresp.ttl = 7d;
    #unset beresp.http.set-cookie;
    }
    return (deliver);
    }

    sub vcl_deliver {
    #set resp.http.x-hits = obj.hits;
    remove resp.http.X-Varnish;
    remove resp.http.Via;
    remove resp.http.Age;
    remove resp.http.X-Pingback;
    set     resp.http.Server = "ZhengJie Web Server";
    set    resp.http.X-Via = "Varnish Cache Server V3.03";
    #if (obj.hits > 0) {
    #set resp.http.X-Cache = "Hit From CDN.IYISM.COM"; }
    #else {
    #set resp.http.X-Cache = "Miss From CDN.IYISM.COM"; }
    #set resp.http.Server = "IYISM";
    return (deliver);
    }

最后,将nginx配置文件中所有的listen 80改成listen 127.0.0.1:80,改完之后重启nginx,然后执行如下命令就ok了~

 代码如下 复制代码
    service varnish restart

说明:Varnish编译安装方法仿自诡谲(icodex.org),配置文件参照

热门栏目