O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Started with Varnish Cache

Book Description

How long does it take for your website to load? Web performance is just as critical for small and medium-sized websites as it is for massive websites that receive tons of hits. Before you pour money and time into rewriting your code or replacing your infrastructure, first consider a reverse-caching proxy server like Varnish. With this practical book, you’ll learn how Varnish can give your website or API an immediate performance boost.

Table of Contents

  1. Preface
    1. Conventions Used in This Book
    2. O’Reilly Safari
    3. How to Contact Us
    4. Acknowledgments
  2. 1. What Is Varnish Cache?
    1. Why Does Web Performance Matter?
    2. Where Does Varnish Fit In?
    3. The Varnish Cache Open Source Project
    4. How Does Varnish Work?
    5. Caching Is Not a Trick
    6. Conclusion
  3. 2. Go, Go, Go and Get Started!
    1. Installing Varnish
      1. Installing Varnish Using a Package Manager
      2. Installing Varnish on Ubuntu and Debian
      3. Installing Varnish on Red Hat and CentOS
    2. Configuring Varnish
      1. The Configuration File
      2. Some Remarks on Systemd on Ubuntu and Debian
      3. Startup Options
    3. What About TLS/SSL?
    4. Conclusion
  4. 3. Varnish Speaks HTTP
    1. Idempotence
    2. State
    3. Expiration
      1. The Expires Header
      2. The Cache-Control Header
      3. Expiration Precedence
    4. Conditional Requests
      1. ETag
      2. Last-Modified
      3. How Varnish Deals with Conditional Requests
    5. Cache Variations
    6. Varnish Built-In VCL Behavior
      1. When Is a Request Considered Cacheable?
      2. When Does Varnish Completely Bypass the Cache?
      3. How Does Varnish Identify an Object?
      4. When Does Varnish Cache an Object?
      5. What Happens if an Object Is Not Stored in Cache?
      6. How Long Does Varnish Cache an Object?
    7. Conclusion
  5. 4. The Varnish Configuration Language
    1. Hooks and Subroutines
      1. Client-Side Subroutines
      2. Backend Subroutines
      3. Initialization and Cleanup Subroutines
      4. Custom Subroutines
    2. Return Statements
    3. The execution flow
    4. VCL Syntax
      1. Operators
      2. Conditionals
      3. Comments
      4. Scalar Values
      5. Regular Expressions
      6. Functions
      7. Includes
      8. Importing Varnish Modules
    5. Backends and Health Probes
    6. Access Control Lists
    7. VCL Variables
    8. Varnish’s Built-In VCL
    9. A Real-World VCL File
    10. Conclusion
  6. 5. Invalidating the Cache
    1. Caching for Too Long
    2. Purging
    3. Banning
      1. Lurker-Friendly Bans
      2. More Flexibility
      3. Viewing the Ban List
    4. Banning from the Command Line
    5. Forcing a Cache Miss
    6. Cache Invalidation Is Hard
    7. Conclusion
  7. 6. Dealing with Backends
    1. Backend Selection
    2. Backend Health
    3. Directors
      1. The Round-Robin Director
      2. The Random Director
      3. The Hash Director
      4. The Fallback Director
    4. Grace Mode
      1. Enabling Grace Mode
    5. Conclusion
  8. 7. Improving Your Hit Rate
    1. Common Mistakes
      1. Not Knowing What Hit-for-Pass Is
      2. Returning Too Soon
      3. Purging Without Purge Logic
      4. No-Purge ACL
      5. 404 Responses Get Cached
      6. Setting an Age Header
      7. Max-age Versus s-maxage
      8. Adding Basic Authentication for Acceptance Environments
      9. Session Cookies Everywhere
      10. No Cache Variations
    2. Do You Really Want to Cache Static Assets?
    3. URL Blacklists and Whitelists
    4. Decide What Gets Cached with Cache-Control Headers
    5. There Will Always Be Cookies
      1. Admin Panel
      2. Remove Tracking Cookies
      3. Remove All But Some
      4. Cookie Variations
    6. Sanitizing
      1. Removing the Port
      2. Query String Sorting
      3. Removing Google Analytics URL Parameters
      4. Removing the URL Hash
      5. Removing the Trailing Question Mark
    7. Hit/Miss Marker
    8. Caching Blocks
      1. AJAX
      2. Edge Side Includes
      3. Making Varnish Parse ESI
      4. ESI versus AJAX
      5. Making Your Code Block-Cache Ready
    9. An All-in-One Code Example
    10. Conclusion
  9. 8. Logging, Measuring, and Debugging
    1. Varnishstat
      1. Example Output
      2. Displaying Specific Metrics
      3. Output Formatting
    2. Varnishlog
      1. Example Output
      2. Filtering the Output
    3. Varnishtop
    4. Conclusion
  10. 9. What Does This Mean for Your Business?
    1. To CDN or Not to CDN
    2. VCL Is Cheaper
    3. Varnish as a Building Block
    4. The Original Customer Case
    5. Varnish Plus
    6. Companies Using Varnish Today
      1. NU.nl: Investing Early Pays Off
      2. SFR: Build Your Own CDN
      3. Varnish at Wikipedia
      4. Combell: Varnish on Shared Hosting
    7. Conclusion
  11. 10. Taking It to the Next Level
    1. What About RESTful Services?
      1. Patch Support
      2. Authentication
      3. Invalidation
    2. Extending Varnish’s Behavior with VMODs
      1. Finding and Installing VMODs
      2. Enabling VMODs
      3. VMODs That Are Shipped with Varnish
    3. Need Help?
    4. The Future of the Varnish Project
  12. Index