/ 中存储网

CentOS5.5下varnish的配置

2014-04-17 16:20:01 来源:ITJS.CN
1.系统更新

yum update

2.安装ncurses(主要是用于安装varnishstat等几个命令用)

Yum -y install ncurses-devel

3.安装所需软件pcre

./configure  --prefix=/usr/local/pcre

Make

Make install

4.安装telnet  telnet-server (在管理控制varnish时需要用到起)

Yum -y install telnet-server

5.创建www用户和组,以及vasrnish缓存文件存放目录

6.创建varnish日志目录

Mkdir -p /var/log/varnish

Chmod +w /var/log/varnish

Chown -R www:www /var/log/varnish

7.编译安装varnish

Cd  /usr/local/src

tar -xvzf varnish-2.1.2.tar.gz

cd varnish-2.1.2

export PKG_CONFIG_PATH=/usr/local/pcre/lib/pkgconfig

./configure -prefix=/usr/local/varnish

Make &&make install

8.创建varnish配置文件

Vi  /usr/local/varnish/vcl.conf

backend rzmsserver { 

.host = "192.168.1.27"; 

.port = "80"; 

.probe = {

.timeout = 50 ms;

.interval = 5s;

.window = 10;

.threshold = 8;

.request =

"GET /UserLogin.aspx HTTP/1.1"

"Host: 192.168.1.27"

"Connection: close"

"Accept-Encoding: foo/bar" ;

}

}

acl purge {

“localhost”;

“127.0.0.1”;

"192.168.1.27"/24;

}

sub vcl_recv {

if (req.request == "PURGE") {

if (!client.ip ~ purge) {

error 405 "Not allowed.";

}

return(lookup);

}

if (req.http.host ~ "^192.168.1.27") {

set req.backend = rzmsserver; 

if (req.request != "GET" && req.request != "HEAD") {

return(pipe);

}

else {

return(lookup);

}

}

else {

error 404 "rzms Cache Server"; 

return(lookup);

}

}

sub vcl_hit {

if (req.request == "PURGE") {

set obj.ttl = 0s;

error 200 "Purged.";

}

}

sub vcl_miss {

if (req.request == "PURGE") {

error 404 "Not in cache.";

}

}

sub vcl_fetch {

if (req.request == "GET" && req.url ~ ".(txt|js)$") {

set beresp.ttl = 3600s;

}

else {

set beresp.ttl = 30d;

}

}

说明:

(1)、Varnish通过反向代理请求后端IP为192.168.0.5,端口为80的web服务器;

(2)、Varnish允许localhost、127.0.0.1、192.168.0.***三个来源IP通过PURGE方法清除缓存;

(3)、Varnish对域名为192.168.1.27的请求进行处理,非192.168.1.27域名的请求则返回“rzms Cache Server”;

(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;

(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

9.启动Varnish

ulimit -SHn 51200

/usr/local/varnish/sbin/varnishd -n /var/vcache -f/ usr/local/varnish/default.vcl -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1024m -p user=www -p group=www -w 30000,51200,10 -T 127.0.0.1:3500 (-p client_http11=on)未使用

说明:

-f 指定配置文件启动 

-a 监听本机的网卡的80端口

-T 指定本机的varnish管理端口

-s file 指定varnish缓存文件的位置以及大小

-w 指处理的最小请求数、最大请求数、超时时间

-g 组名

-u 用户名

-p client_http11=on 支持http1.1协议

-P(大P) /usr/local/varnish/var/varnish.pid 指定其进程码文件的位置,实现管理。

10.varnishncsa用来将Varnish访问日志写入日志文件

/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish/varnish.log &

11.通过 Varnish 管理端口進行管理(用 -help查看可用的指令)

/usr/local/varnish/varnishadm -T 127.0.0.1:3500 help

(1) 例如:清除具体URL地址:

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/

(2) 例如:清除具体URL地址:

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$

(3) 例如:清除所有缓存:

/usr/local/varnish-2.1/bin/varnishadm -T 127.0.0.1:3500 url.purge *$

12.通过varnishstat监控varnish状态

/usr/local/varnish/bin/varnishstat -n var/vcache