Varnish安装

cat << EOF >> /etc/yum.repos.d/varnish.repo
[varnish]
name=Varnish for Enterprise Linux 6
baseurl=https://repo.varnish-cache.org/redhat/varnish-4.0/el6/
enabled=1
gpgkey=https://repo.varnish-cache.org/GPG-key.txt
gpgcheck=1
EOF


yum install -y varnish

Configure Varnish

There are two configuration files to edit, /etc/sysconfig/varnish and /etc/varnish/default.vcl.

Assuming you want Varnish to run on port 80, open /etc/sysconfig/varnish and change VARNISH_LISTEN_PORT to the following:

VARNISH_LISTEN_PORT=80

Next, open  /etc/varnish/default.vcl  and use the following configuration. Be sure to replace  PUBLIC_IP_ADDRESS  with the IP address of your web servers.
vcl 4.0;

import directors;

backend web1 {
    .host = "PUBLIC_IP_ADDRESS";
    .port = "80";
    .probe = {
        .url = "/";
        .interval = 5s;
        .timeout = 1s;
        .window = 5;
        .threshold = 3;
    }
}

backend web2 {
    .host = "PUBLIC_IP_ADDRESS";
    .port = "80";
    .probe = {
        .url = "/";
        .interval = 5s;
        .timeout = 1s;
        .window = 5;
        .threshold = 3;
    }
}

sub vcl_init {
    new apache = directors.round_robin();
    apache.add_backend(web1);
    apache.add_backend(web2);
}

sub vcl_recv {
    set req.backend_hint = apache.backend();

    unset req.http.Cookie;

    # Do not cache listed file extensions
    if (req.url ~ "\.(zip|sql|tar|gz|tgz|bzip2|bz2|mp3|mp4|m4a|flv|ogg|swf|aiff|exe|dmg|iso|box|qcow2)") {
        set req.http.X-Cacheable = "NO:nocache file";
        return (pass);
    }
}

sub vcl_backend_response {
    # Set cached objects to expire after 1 hour instead of the default 120 seconds.
    set beresp.ttl = 1h;
}

sub vcl_deliver {
    if (obj.hits > 0) {
           set resp.http.X-Cache = "HIT";
    } else {
           set resp.http.X-Cache = "MISS";
    }
}

Enable and Start the varnish Service

Enable the  varnish  service on boot:

chkconfig varnish on

Finally, start the varnish service:

service varnish start

Verify Varnish is Working

Assuming everything is working properly, and have changed your website’s DNS entries to point to the new Varnish server, you should be able to browse to any of your websites in a web browser without issue.

If you have not yet changed your website’s DNS entries to point to the new Varnish server, you can still verify Varnish is working by running command curl -i -H 'Host: example.com' varnish.example.com | less (example.com is your website’s domain name and varnish.example.com is the hostname of the new Varnish server). At the top of the output should be a header called X-Cache. The value of this header will probably be MISS. If you run the curl command again, the value should now beHIT because Varnish cached the web page after you ran the first curl command.

Once you change your website’s DNS entries to point to the new Varnish server, you can run a similar curl command,curl -i http://example.com | less, to verify Varnish is working.

In addition, you can monitor what Varnish is doing by running varnishlogvarnishhistvarnishstat, and varnishtop on the server running the varnish service.

Troubleshooting

If you have problems starting the varnish service, you will find there isn’t much information to troubleshoot with. I ran into a situation where service varnish start simply failed with no debug output.

Output from /var/log/messages only showed No VCL available. In addition, running varnishd -d also only showed No VCL available. Not very useful.

Luckily, you can get much more useful debugging information by running varnishd -C -f /etc/varnish/default.vcl. This actually told me where my syntax error was.

安装后服务不能启动解决办法:

关闭防火墙 chkconfig iptables off,关闭SElinux: vi /etc/selinux/config 将SELINUX=enforcing改为SELINUX=disabled

curl -i -H 'Host: 192.168.3.66' 192.168.3.59 | less




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值