标签归档:varnish

CentOS下Varnish安装

只需要以下两步:

    1. rpm –nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el6/noarch/varnish-release/varnish-release-3.0-1.el6.noarch.rpm
    2. yum install varnish

此时,varnish已经安装完毕,可以使用service varnish start|stop|restart进行启动、停止、重启

varnish主要配置文件:

    1. /etc/sysconfig/varnish(限CentOS和RedHat),可以修改默认端口号,以及要加载的配置文件
    2. /etc/varnish/default.vcl,可以配置要代表的host 及 port,可以重写varnish的相关函数,如通过header传递client_ip,设置要过滤的地址,如wordpress则要将wp-admin, wp-login进行过滤,不然会出现不能正常发文章的情况
    3. 修改/etc/sysconfig/varnish要使用service varnish restart,修改/etc/varnish/default.vcl可以使用service varnish restart|reload

常见重写函数:

    1. 重写vcl_recv,将client.ip拼接到req.http.X-Forwarded-For,应用程序可以从header中读取X-Forwarded-For来获取真实IP地址
    2. 重写vcl_recv,根据host来判断要过滤的url,return(pass);

代码如下:

default {.host = "127.0.0.1";.port = "8080";
    }
    sub vcl_recv {
        if (req.restarts == 0) {
            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.url~"wp-(login|admin)")) {
            unset req.http.cookie;
        } else {
            return (pass);
        }
        return (lookup);
    }
    sub vcl_fetch {
        if (! (req.url~"wp-(login|admin)")) {
            unset beresp.http.set - cookie;
        }
    }