Varnish2.1.4配置缓存服务器

发布时间:2020-06-08编辑:脚本学堂
Varnish2.1.4配置缓存服务器
 
varnish安装配置简单,支持后端服务器的负载均衡和健康检查,url地址重写。
官方下载地址:
wget http://www.varnish-software.com/sites/default/files/varnish-2.1.4.tar.gz
 
标准安装:
yum -y install automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
cd varnish-2.1.4
./configure --prefix=/usr/local/varnish
make && make install
简单配置文件:cat /usr/local/varnish/etc/varnish/default.vcl
复制代码 代码如下:
backend default {
     .host = "127.0.0.1";
     .port = "9999";
}
#定义后端应用端口
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 ~ ".(png|gif|jpg|swf|css|js)$") {
         return (lookup);
     }
     return (pass);
}
#图片、swf等静态文件设置缓存,其他类型文件pass到后端.
sub vcl_pipe {
      set bereq.http.connection = "close";
     return (pipe);
}
sub vcl_pass {
     return (pass);
}
启动Varnish:
/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,128M -T 127.0.0.1:2500 -a 0.0.0.0:80
查看状态:
/usr/local/varnish/bin/varnishstat
查看Referer:
/usr/local/varnish/bin/varnishtop -i rxheader -I Referer
查看访问路径:
/usr/local/varnish/bin/varnishtop -i rxurl

配置文件说明:
(1)、Varnish通过反向代理请求后端IP为74.82.170.235,端口为80的web服务器;
(2)、Varnish允许localhost、127.0.0.1、74.82.170.235三个来源IP通过PURGE方法清除缓存;
(3)、Varnish对域名为www.linuxidc.com的请求进行处理,非www.linuxidc.com域名的请求则返回“Cai Yuan Zhi Cache Server”;
(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。
# 创建缓存目录和日志文件目录
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache
mkdir -p /var/log/varnish
chmod +w /var/log/varnish
chown -R www:www /var/log/varnish
# 启动Varnish
# /usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
参数说明:
-n 缓存写入路径
-f 指定配置文件启动
-a 监听本机的网卡的80端口
-T 指定本机的varnish管理端口
-s file 指定varnish缓存文件的位置以及大小
-w 指处理的最小请求数、最大请求数、超时时间
-g 组名
-u 用户名
-p client_http11=on 支持http1.1协议
-P 指定其进程码文件的位置
# 启动varnishncsa用来将Varnish访问日志写入日志文件
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/log/varnish/varnish.log &
# 通过 Varnish 管理端口進行管理
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help
# 清除具体URL地址
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/
# /usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$
# 清除所有缓存
# /usr/local/varnish-2.1/bin/varnishadm -T 127.0.0.1:3500 url.purge *$
# 通过varnishstat监控varnish状态
/usr/local/varnish/bin/varnishstat –n var/vcache
---------------------------------------------------------------
#如果是虚拟机环境,参照以下代码:
复制代码 代码如下:
backend www {
             set backend.host = "www.example.com";
             set backend.port = "80";
         }
backend images {
             set backend.host = "images.example.com";
             set backend.port = "80";
 }
sub vcl_recv {
             if (req.http.host ~ "^(www.)?example.com$") {
                 set req.backend = www;
             } elsif (req.http.host ~ "^images.example.com") {
                 set req.backend = images;
             } else {
                 error 404 "Unknown virtual host";
             }
         }
    #关于cache存在时间设置
sub vcl_fetch {
             if (obj.ttl < 120s) {
                 set obj.ttl = 120s;
             }
         }