+ Reply to Thread
Results 1 to 10 of 24
Thread: Varnish is my new best friend
-
03-29-2012 11:08 AM #1
Varnish is my new best friend
How would you guys feel about increasing the speed of your pages by 10X and reducing bandwidth by that same amount? Forget memcache, take a look at varnish. I'm testing it on theNetPool and so far it has cut page load times from 4.5-4.8 seconds down to 400-500ms. You can specify url's and such to skip caching, so in my case I skip anything with a post variable. This allows our search bar to work properly, and the results pages are still returned from cache if they exist.
Installation is pretty darn easy as well.
If you are on centos:
Now, you will need to change the port used for apache. Head on over to your httpd.conf file. /etc/httpd/httpd.confPHP Code:yum install varnish
Near the top of the file you should have:Change that to:PHP Code:Listen 80
you also need to edit all of your virtual hosts and change :80 to :8080 on all of them. I usually just use find and replace.PHP Code:Listen 8080
Now restart apache:
all of your sites will be down, this is because we need varnish to run on port 80 and intercept incoming apache requests.PHP Code:service httpd restart
Now to edit your varnish config file usually at /etc/varnish/default.vlc. Here is what I am using:
Be sure to change the line near the top: .host = "change to your servers ip address"; to your actual server ip.PHP Code:# 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 = "change to your servers ip address";
.port = "8080";
}
/*
*
* Next, configure the "receive" subroutine.
*
*/
sub vcl_recv {
# do not cache POST requests
if (req.request == "POST") {
return (pipe);
}
# Use the backend we set up above to answer the request if it's not cached.
set req.backend = default;
# Pass the request along to lookup to see if it's in the cache.
return(lookup);
}
/*
*
* Next, let's set up the subroutine to deal with cache misses.
*
*/
sub vcl_miss {
# We're not doing anything fancy. Just pass the request along to the
# subroutine which will fetch something from the backend.
return(fetch);
}
/*
*
* Now, let's set up a subroutine to deal with cache hits.
*
*/
sub vcl_hit {
# Again, nothing fancy. Just pass the request along to the subroutine
# which will deliver a result from the cache.
return(deliver);
}
/*
*
* This is the subroutine which will fetch a response from the backend.
* It's pretty fancy because this is where the basic logic for caching is set.
*
*/
sub vcl_fetch {
# Get the response. Set the cache lifetime of the response to 1 hour.
set beresp.ttl = 1h;
# Indicate that this response is cacheable. This is important.
set beresp.http.X-Cacheable = "YES";
# Some backends *cough* Django *cough* will assign a Vary header for
# each User-Agent which visits the site. Varnish will store a separate
# copy of the page in the cache for each instance of the Vary header --
# one for each User-Agent which visits the site. This is bad. So we're
# going to strip away the Vary header.
unset beresp.http.Vary;
# Now pass this backend response along to the cache to be stored and served.
return(deliver);
}
/*
*
* Finally, let's set up a subroutine which will deliver a response to the client.
*
*/
sub vcl_deliver {
# Nothing fancy. Just deliver the goods.
# Note: Both cache hits and cache misses will use this subroutine.
return(deliver);
}
This is a bare bones config, the only thing I have added "special" is this:
which simply stops varnish from caching things sent via POST (in our case our search form). You can tell varnish which url's to skip and documentation is all over the web, although I will try to help you here if you run into a specific problem.PHP Code:# do not cache POST requests
if (req.request == "POST") {
return (pipe);
}
Now that we have a basic config file, we need to start varnish on port 80:
go to /etc/sysconfig/varnish
Find:
replace with:PHP Code:VARNISH_LISTEN_PORT=6081
Save and start varnish:PHP Code:VARNISH_LISTEN_PORT=80
check your site and make sure it still works. You can monitor varnish with: varnishstat and varnishhist from the command line to make sure it is working. In varnishhist #'s are misses and |'s are hits.PHP Code:service varnish start
-
03-29-2012 12:58 PM #2
Here is what i've come up with finally that allows for logins, registration, and our connect with facebook/twitter functionality, as well as allowing signups. THis should give you an idea of what you can do to exclude certain url's:
PHP Code:* Next, configure the "receive" subroutine.
*
*/
sub vcl_recv {
# do not cache POST requests
if (req.request == "POST") {
return (pipe);
}
#we should not cache any page for user backend
if (req.url ~ "^/account" || req.url ~ "^/account") {
return (pass);
}
#we should not cache any page for registration
if (req.url ~ "^/register" || req.url ~ "^/register") {
return (pass);
}
#we should not cache any page for registration
if (req.url ~ "^/login" || req.url ~ "^/login") {
return (pass);
}
# Use the backend we set up above to answer the request if it's not cached.
set req.backend = default;
# Pass the request along to lookup to see if it's in the cache.
return(lookup);
}
-
03-29-2012 01:57 PM #3Senior Member
- Join Date
- Apr 2011
- Posts
- 817
Um, so this is a replacement for Memcache or should I still run Memcache and/or file based caching?
How much memory does this use?
-
03-29-2012 02:11 PM #4
I would ditch memcache and file cache and use this instead from my results thus far. Memory is up to you. You can run it on disk if you want as well. I'm running it on a spinning disk with 100GB set aside. An ssd would be awesome to use, but either way it's fast.
-
03-29-2012 02:19 PM #5Senior Member
- Join Date
- Apr 2011
- Posts
- 817
How would you configure this for multiple IP addresses? Currently I have several small (256mb 10gb) linux virtual instances with their own IP's which I point to a Memcache server with 1gb of RAM. I'll consider getting 1 big server to handle everything IF i'll be able to run multiple IP's for my sites?
-
03-29-2012 03:00 PM #6
I haven't tried, but i'm sure it is well documented on their site or on the web.
-
03-29-2012 03:28 PM #7
By the way, you could easily have varnish on a separate machine. I'm using a dedicated box for varnish, and one for apache.
-
03-30-2012 12:42 AM #8Senior Member
- Join Date
- Apr 2011
- Posts
- 817
So I could set it up like I did my Memcache server, but instead of telling the application to use Memcache, I tell Apache to use Varnish.... mmm, need to go read up on the doc's
-
03-30-2012 05:57 AM #9Performer
- Join Date
- May 2011
- Posts
- 26
Do we need to open 8080 in apf?
-
03-30-2012 07:26 AM #10

Reply With Quote
