天天看点

varnish安装配置

<a href="http://www.woyaoai.net/" target="_blank">爱慕尔商城!</a>

centos/redhat

1、安装

<code>#rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm</code>

<code>#yum install varnish</code>

2、配置

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 = "192.168.75.122";

.port = "80";

.probe = {

 .url = "/";

 .interval = 5s;

 .timeout = 1 s;

 .window = 5;

 .threshold = 3;

}

backend test {

.host = "192.168.0.83";

#.probe = {

# .url = "/";

# .interval = 5s;

# .timeout = 1 s;

# .window = 5;

# .threshold = 3;

# }

#director ecae_director round-robin {

#{

# .backend = default;

#}

# .backend = test;

acl purge {

"localhost";

"192.168.210.0"/24;

"192.168.51.0"/24;

# Below is a commented-out copy of the default VCL logic. If you

# redefine any of these subroutines, the built-in logic will be

# appended to your code.

sub vcl_recv {

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

 if (!client.ip ~ purge) {

 error 405 "Not allowed.";

 }

 return(lookup);

if (req.http.host ~ "(?i)^(images.)?test.cn$") {

 set req.http.host = "images.test.cn";

 set req.backend = test;

} elsif (req.http.host ~ "(?i)^images2.test.cn$") {

 set req.http.host = "images2.test.cn";

} else {

 error 404 "Unknown virtual host";

if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {

 unset req.http.cookie;

 set req.url = regsub(req.url, "\?.*$", "");

if (req.http.cookie) {

if (req.http.Accept-Encoding) {

 if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {

 # No point in compressing these

 remove req.http.Accept-Encoding;

 } elsif (req.http.Accept-Encoding ~ "gzip") {

 set req.http.Accept-Encoding = "gzip";

 } elsif (req.http.Accept-Encoding ~ "deflate") {

 set req.http.Accept-Encoding = "deflate";

 } else {

 # unknown algorithm

return (lookup);

#

sub vcl_hit {

set obj.ttl = 0s;

error 200 "Purged.";

sub vcl_miss {

error 404 "Not in cache.";

sub vcl_hash {

hash_data(req.url);

if (req.http.host) {

 hash_data(req.http.host);

 hash_data(server.ip);

return (hash);

sub vcl_fetch {

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

unset beresp.http.set-cookie;

set beresp.ttl = 365d;

sub vcl_deliver {

if (obj.hits &gt; 0) {

set resp.http.X-Cache = "HIT";

set resp.http.X-Cache = "MISS";

3、启动

varnishd -f /etc/varnish/default.vcl -s malloc,3G -w 8,500,300 -T 127.0.0.1:2000 -a 0.0.0.0:80

<a href="http://www.055680.com/" target="_blank">城市物语</a>

    本文转自yuangang_love 51CTO博客,原文链接:http://blog.51cto.com/linux008/1195549,如需转载请自行联系原作者