Join Free
  • Inbox
    + Reply to Thread
    Page 1 of 3 1 2 3 LastLast
    Results 1 to 10 of 24
    1. #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:

      PHP Code:
      yum install varnish 
      Now, you will need to change the port used for apache. Head on over to your httpd.conf file. /etc/httpd/httpd.conf

      Near the top of the file you should have:
      PHP Code:
      Listen 80 
      Change that to:
      PHP Code:
      Listen 8080 
      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.

      Now restart apache:

      PHP Code:
      service httpd restart 
      all of your sites will be down, this is because we need varnish to run on port 80 and intercept incoming apache requests.

      Now to edit your varnish config file usually at /etc/varnish/default.vlc. Here is what I am using:

      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);

      Be sure to change the line near the top: .host = "change to your servers ip address"; to your actual server ip.

      This is a bare bones config, the only thing I have added "special" is this:

      PHP Code:
       # do not cache POST requests
        
      if (req.request == "POST") {
          return (
      pipe);
        } 
      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.

      Now that we have a basic config file, we need to start varnish on port 80:

      go to /etc/sysconfig/varnish

      Find:

      PHP Code:
      VARNISH_LISTEN_PORT=6081 
      replace with:

      PHP Code:
      VARNISH_LISTEN_PORT=80 
      Save and start varnish:

      PHP Code:
      service varnish start 
      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.

    2. #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:
      Nextconfigure 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);


    3. #3
      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?

    4. #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.

    5. #5
      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?

    6. #6
      I haven't tried, but i'm sure it is well documented on their site or on the web.

    7. #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.

    8. #8
      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

    9. #9
      Do we need to open 8080 in apf?

    10. #10
      Quote Originally Posted by Ekim View Post
      Do we need to open 8080 in apf?
      Yes, if you have it blocked

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts