Varnish is a high-performance open-source HTTP accelerator that can be used for purely proxy servers, load balancing, but Varnish's primary function is cache acceleration, which is also the best place for it. The following describes how to install and use.
First, the environment
# cat/etc/issue
CentOS Release 6.3 (Final)
Kernel \ r on an \m
# getconf Long_bit
64
Second, download
cd/usr/local/src/
wget http://repo.varnish-cache.org/source/varnish-3.0.1.tar.gz
Tar xzvf varnish-3.0.1.tar.gz
Third, installation
CD varnish-3.0.1
Yum install-y automake autoconf libtool ncurses-devel libxslt Groff pcre-devel pkgconfig
./configure--prefix=/usr/local/varnish
Make
Make install
Four, calibration installation
cd/usr/local/varnish/sbin/
./varnishd-v
Five, configuration
# cd/usr/local/varnish/etc/varnish/
# CP DEFAULT.VCL Default.vcl.bak
# > DEFAULT.VCL
# Cat DEFAULT.VCL
# This was a basic VCL configuration file for varnish. See the VCL (7)
# Mans page for details on VCL syntax and semantics.
#
# Default Backend definition. Set this to point to your content
# server.
#
Backend Default {
. Host = "115.28.225.216";
. Port = "80";
# # #下面三行为新加配
. connect_timeout = 1s;
. first_byte_timeout = 5s;
. between_bytes_timeout = 2s;
}
#
# Below is a commented-out copy of the default VCL logic. If You
# Redefine any of these subroutines, the built-in logic would be
# appended to your code.
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);
}
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 The first request to the backend would have
# # X-forwarded-for Set. If x-forwarded-for and want to
# # has it set for all requests and make sure to has:
# # Set bereq.http.connection = "Close";
# # here. It isn't 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 <= 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);
}
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" >
#
#
# <title> "} + Obj.status +" "+ Obj.response + {" </title>
#
# <body>
#
# <p> "} + obj.response + {" </p>
#
# <p>xid: "} + Req.xid + {" </p>
#
# <p>varnish Cache server</p>
# </body>
#
# "};
# return (deliver);
# }
#
Sub Vcl_init {
return (OK);
}
#
Sub Vcl_fini {
return (OK);
}
Six, start and close varnish
/usr/local/varnish/sbin/varnishd-f/usr/local/varnish/etc/varnish/default.vcl-s malloc,1024m-t 127.0.0.1:200-a 0.0.0.0:80
Introduction to Startup Parameters:
-f/usr/local/etc/varnish/default.vcl
This –f option specifies which configuration file varnishd uses.
-S MALLOC,1G
This –s option is used to determine the storage type and storage capacity used by varnish, I am using the malloc type (malloc is a C function for allocating memory space), and 1G defines how much memory is malloced,1g = 1gigabyte.
-T 127.0.0.1:2000
Varnish has a text-based management interface that can be activated to manage varnish without stopping varnish. You can specify which interface the management software listens to. Of course you can't allow people all over the world to access your varnish management interface, because they can easily access the varnish management interface to gain access to your root. I recommend that you just let it listen to the native port. If you have users in your system that you do not fully trust, you can restrict access to varnish's management ports through firewall rules.
-A 0.0.0.0:8080
The meaning of this sentence is to make varnish listen to all IP to 8080 port HTTP request, if in production environment, you should let varnish listen 80, this is also the default.
Pkill varnishd//Close varnish
/usr/local/varnish/bin/varnishncsa-w/var/log/varnish.log &// startup Varnishncsa to write the varnish access log to the log file;
This article is from the "Square Hill" blog, please be sure to keep this source http://523514.blog.51cto.com/513514/1543007