Install the latest version of varnish 3.0 on the Intranet VPs for testing.
Varnish has rpm in centos and Deb Binary Package download in Ubuntu, but I chose to compile and install the source code.
Download firstWget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
Tar-xzvf varnish-3.0.0.tar.gz
Yum install gcc
Yum install gcc-C ++ libstdc ++-devel
Yum install-y httpd-devel PCRE Perl PCRE-devel zlib-devel geoip-devel
Varnish requires PCRE support. If PCRE-devel is not installed on the server, a message is displayed, indicating the Prce directory.
Use Yum install PCRE-devel on the centos Server
Decompress varnish for compilation and Installation
CD Varnish-3.0.0
./Autogen. Sh
./Configure -- prefix =/usr/local/varnish pkg_config_path =/usr/lib/pkgconfig
Make
Make install
After the installation is successful, the installation directory is
CD/usr/local/Varnish/sbin
./Varnishd-V
Check the version to see if the installation is successful.
Test Varnish
Edit the configuration file first
VI/usr/local/Varnish/etc/Varnish/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.88.156 ";
. Port = "80 ";
### The following three lines are newly added
. Connect_timeout = 1 s;
. First_byte_timeout = 5S;
. Between_bytes_timeout = 2 S;
}
#
# 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. restarts = 0 ){
If (req. http. X-forwarded-){
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 );
}
If (req. Request! = "Get" & Req. Request! = "Head "){
/* We only deal with get and head by default */
Return (PASS );
}
If (req. http. Authorization | Req. http. Cookie ){
/* Not cacheable by default */
Return (PASS );
}
Return (lookup );
}
#
Sub vcl_pipe {
# Note that only the first request to the backend will have
# X-forwarded-for set. If you use X-forwarded-for and want
# Have it set for all requests, make sure to have:
# Set bereq. http. Connection = "close ";
# Here. It is not set by default as it might break some broken Web
# Applications, like IIS with NTLM authentication.
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 <= 0 S |
Beresp. http. Set-Cookie |
Beresp. http. Vary = "*"){
/*
* Mark as "Hit-for-Pass" for the next 2 minutes
*/
Set beresp. TTL = 120 s;
Return (hit_for_pass );
}
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 + {" # <P> "} + obj. Response + {" </P>
# <H3> guru meditation: # <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 );
}
/Usr/local/Varnish/sbin/Varnishd-F/usr/local/Varnish/etc/Varnish/Default. VCL-s malloc, 1024 M-T 127.0.0.1: 200-A 0.0.0.0: 80
Now you can open varnish.
Http: // 192.168.88.1/(enable the varnish port number)
Dai tu shows that nginx is a web server running on 177, and varnish appears, indicating that the cache has been successfully set up and is working properly.
Start varnishncsa to write varnish access logs to log files:
/Usr/local/Varnish/bin/varnishncsa-N/var/vcache-W/var/logs/varnish. log &
Configure Automatic Start of varnish upon startup
VI/etc/rc. Local
Add the following content at the end:
Reference ulimit-shn 51200.
/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
/Usr/local/Varnish/bin/varnishncsa-N/var/vcache-W/var/logs/youvideo. log &
Optimize Linux Kernel Parameters
VI/etc/sysctl. conf
Add the following content at the end:
Reference 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
View varnish server connections and hit rate
/Usr/local/Varnish/bin/varnishstat
Configure the startup script:
Mkdir/Cache/Varnish/V-P
VI/usr/local/Varnish/sbin/start. Sh
#! /Bin/sh
# File: Start. Sh
Date-u
/Usr/local/Varnish/sbin/varnishd-A 0.0.0.0: 80-s file,/Cache/Varnish/V, 1024 M-F/usr/local/Varnish/etc/Varnish/default. VCL-P thread_pool_max = 1500-P thread_pools = 5-P listen_depth = 512
Chmod 777 start. Sh
Run:
/Usr/local/Varnish/sbin/start. Sh
Http://www.cnblogs.com/littlehb/archive/2012/02/11/2346319.html