Varnish Configuration Note History

Source: Internet
Author: User
Tags varnish

Varnish is an open source reverse proxy software and HTTP accelerator, compared with traditional squid, varnish has a higher performance, faster, easier to manage and many other advantages, many large operating sites are beginning to try to replace squid with varnish, All these have prompted varnish to develop rapidly.

1, preparation work and download source package

Yum install-y automake autoconf libtool ncurses-devel libxslt Groff pcre-devel pkgconfig

wget http://repo.varnish-cache.org/source/varnish-3.0.3.tar.gz

2, installation

Tar zxf varnish-3.0.3.tar.gz

CD varnish-3.0.3

./autogen.sh

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

Make && make install

3, add varnishd process users www, user group www, create/var/vcache directory, so that WWW users have access to read and write

Groupadd www

Useradd www-g www

Mkdir/home/vcache

Chown-r Www:www/home/vcache

Chmod-r 750/home/vcache

4. Edit/etc/sysctl.conf optimize several kernel parameters

Net.ipv4.tcp_fin_timeout = 30

Net.ipv4.tcp_keepalive_time = 300

Net.ipv4.tcp_syncookies = 1

Net.ipv4.tcp_tw_reuse = 1

Net.ipv4.tcp_tw_recycle = 1

Net.ipv4.ip_local_port_range = 5000 65000

Run sysctl-p reset kernel parameters by configuration file

5, Start varnishd

/usr/local/varnish/sbin/varnishd-u www-g www-f/usr/local/varnish/etc/varnish/varnish.conf-a 0.0.0.0:80-s file,/ Home/vcache/varnish_cache.data,100m-w 1024,8192,10-t 3600-t 127.0.0.1:3500

Parameter description:

-U to run with what

What group-G runs

-F Varnish configuration file

-A binding IP and port

-S varnish cache file location and size

-W min, max thread and timeout time

-T varnish management port, mainly used to clear the cache

-P Client_http11=on Support http1.1 protocol

-P (Big P)/usr/local/varnish/var/varnish.pid specifies the location of its process code file, implementing management

6. The startup VARNISHNCSA is used to write the varnish access log to the log file:

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

7, varnish cache cleanup

/usr/local/varnish/bin/varnishadm-t 192.168.1.180:3500 purge "req.http.host ~ www.5013.org$ && Req.url ~/ Static/image/tp.php "

Description

192.168.1.180:3000 Cache server address for cleanup

Www.5013.org is the domain name that is cleared

/static/image/tp.php is a list of cleared URL addresses

Clear all Caches

/usr/local/varnish/bin/varnishadm-t 192.168.1.180:3500 Url.purge *$

Clear all caches under Image directory

/usr/local/varnish/bin/varnishadm-t 192.168.1.180:3500 url.purge/image/

8, will join the startup item

Vi/etc/rc.local

Ulimit-shn 51200

/usr/local/varnish/sbin/varnishd-u www-g www-f/usr/local/varnish/etc/varnish/varnish.conf-a 0.0.0.0:80-s file,/ Home/vcache/varnish_cache.data,100m-w 1024,8192,10-t 3600-t 127.0.0.1:3500

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

9. Kill the VARNISHD process

Pkill varnishd

10, view varnishd hit rate

/usr/local/varnish/bin/varnishstat

11, update the system time

Yum Install-y NTP

Ntpdate time.nist.gov

echo "* * * ntpdate time.nist.gov" "/etc/crontab

Attachment multi-Host multi-domain varnish.conf configuration

Backend Blog {

. Host = "198.56.193.190";

. Port = "80";

}

Backend www {

. Host = "192.168.1.170";

. Port = "80";

}

Sub Vcl_recv {

if (req.http.host ~ "^ (www.)? 5013.org$") {

Set req.backend = blog;

elsif (req.http.host ~ "^ (www.)? (test1.com|test2.com) $ ") {

Set req.backend = www;

} else {

Error 404 "Unknown virtual host";

}

}

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.request!= "Get" &&

Req.request!= "Head" &&

Req.request!= "put" &&

Req.request!= "POST" &&

Req.request!= "TRACE" &&

Req.request!= "Options" &&

Req.request!= "DELETE") {

/* non-rfc2616 or CONNECT which is weird. */

return (pipe);

}

#只有GET与HEAD方法才会使用Lookup, using caching.


if (req.request!= "get" && req.request!= "Head") {


/* We only have deal with get and head by default */


return (pass);


    }


# if (req.http.Authorization | | Req.http.Cookie) {


#/Not cacheable by default */


# return (pass);


    # }


#如果请求的是php页面直接转发到后端服务器


if (req.url ~ ". (php|cgi) ($|?) )") {


return (pass);


    }


return (lookup);


    }


Sub Vcl_pipe {


return (pipe);


    }


Sub Vcl_pass {


return (pass);


    }


Sub Vcl_hash {


Hash_data (req.url);


if (req.http.host) {


Hash_data (req.http.host);


} else {


Hash_data (SERVER.IP);


    }


return (hash);


    }


Sub Vcl_hit {


return (deliver);


    }


Sub Vcl_miss {


return (fetch);


    }


Sub Vcl_fetch {


if (beresp.ttl <= 0s | |


Beresp.http.set-cookie | |


Beresp.http.Vary = = "*") {


    /*


* Mark as "Hit-for-pass" for the next 2 minutes


    */


set beresp.ttl = S;


return (hit_for_pass);


    }


if (req.url ~ ". (png|gif|jpg) $") {


unset Beresp.http.set-cookie;


Set beresp.ttl = 1h;


    }


#设置图片的缓存TTL为一小时


return (deliver);


    }


Sub Vcl_deliver {


return (deliver);


    }


Sub Vcl_error {


Set obj.http.content-type = "text/html; Charset=utf-8 ";


Set obj.http.retry-after = "5";


Synthetic {"


<?xml version= "1.0" encoding= "Utf-8"?>


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en"


"HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-STRICT.DTD" >


<html>


<head>


<title> "} + Obj.status +" "+ Obj.response + {" </title>


</head>


<body>


<h1>error "} + Obj.status +" "+ Obj.response + {" </h1>


<p> "} + obj.response + {" </p>


<h3>guru meditation:</h3>


<p>xid: "} + Req.xid + {" </p>


<hr>


<p>varnish Cache server</p>


</body>


</html>


    "};


return (deliver);


    }


Sub Vcl_init {


return (OK);


    }


Sub Vcl_fini {


return (OK);


    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.