DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Install OroCRM on Ubuntu 20.04
  • Ampere PMU Profiler: A Guide to Microarchitecture Profiling
  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService

Trending

  • Spring AI on Local LLMs
  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software
  • How to Migrate From LangChainGo to Genkit Go
  • The Self-Hosting Math Changed While We Were Still Arguing About Sovereignty

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Edge Side Includes with Varnish in 10 minutes

Edge Side Includes with Varnish in 10 minutes

By 
Giorgio Sironi
·
Aug. 23, 11 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Varnish is a tool built to be an intermediate server in the HTTP chain, not an origin one like Apache or IIS. You can outsource caching, logging, zipping and other filters to Varnish, since they are not the main feature of an HTTP server like Apache.

What we'll see today is how to work with Edge Side Includes in Varnish, as a way to compose dynamic pages from independently generated and cached fragments; we won't encounter logging or other features. If you are familiar with PHP, ESI is an (almost) standard for executing include()-like statements on a front end server like Varnish; the proxy is able not only to assembly pages but also to cache them according to different policies: a certain time, for a single user, and so on.

Thijs Feryn and Alessandro Nadalin introduced me to Varnish and ESI respectively, for the first time. I recommend you to consider their blogs and talks as additional sources on these topics.

Installation

The default version of Varnish in Ubuntu 11.04 is instead 2.1, and apparently does not support ESI very much.

Installation via packages means adding a public key and a repository to your list of software sources, and install the varnish package via apt-get or an equivalent command. You can install version 3.0.0 via packages, but only in Ubuntu LTS (10.04).

A way that always works in these cases is the installation from sources. The linked page will list the package dependencies and give you a sequence of 3-4 commands to seamlessly compile varnish. I used checkinstall instead of make install to get a binary package that I can reuse later:

$ sudo checkinstall -D --install=no --fstrans=no [email protected] --reset-uids=yes --nodoc --pkgname=varnish --pkgversion=3.0.0 --pkgrelease=201108231000 --arch=i386

After installation with dpkg, check that varnishd is available and of the right version:

[10:18:17][giorgio@Desmond:~]$ varnishd -V
varnishd (varnish-3.0.0 revision 3bd5997)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2011 Varnish Software AS

Varnish needs minimal configuration: a server to point at. For our tests you can edit /etc/varnish/default.vcl and check (or add) the following:

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

You can execute ps -A | grep varnishd at any time to see if varnish is already in execution.

Execution

[09:55:18][giorgio@Desmond:~]$ sudo varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:8080
storage_malloc: max size 1024 MB.
  • 1 gigabyte of memory is allocated for keeping fragments in RAM.
  • An administrative interface will respond on port 2000, and only be accessible from localhost.
  • http://localhost:8080/ is the exposed HTTP server, and will point to http://localhost:80 as defined in the configuration.
Look at man varnishd for more switched and to man vcl for additional explanations on the configuration language.

A bit of ESI

ESI is a technique for leveraging HTTP cache and at the same time build dynamic pages.

The problem with today's pages is that they are highly dynamic: some sections change very often or according to the current user (Welcome, John Doe or the current posts timeline); some sections do not change at all for days (the navigation bar and the layout structure); some sections change in response to external events (the list of incoming messages only when a new message arrives).

It would be ideal to set different caching configurations for all the page's fragments. But implementing this strategy in the application code is error-prone and means reinventing the wheel. To use HTTP cache you will be forced to load with Ajax every single fragment of the page, even a single paragraph.

With ESI, your application produces only the pieces, and lets an implementor of the Edge Side Include specification like Varnish assemble the whole thing.

Example

HTML page (very static):

<p>Varnish will work on this page: <esi:include src="/date.php" />.</p>

PHP page (really dynamic, can change at any time):

<?php
echo date('Y-m-d');

Varnish configuration to add:

sub vcl_fetch {
    set beresp.do_esi = true;
}

if you want something more precise:

sub vcl_fetch {
    if (req.url == "/index.html") {
        set beresp.do_esi = true;
    }
}

You can add settings for the ttl and other options for the different pieces.
The result is neat:

<p>Varnish will work on this page: 2011-08-23.</p>

No sign of Varnish interventions, and totally transparent for the client. And sometimes you can also throw away Zend_Layout and similar components to assemble HTML on the PHP side.

Cache (computing) Fragment (logic) application PHP HTML Execution (computing) ubuntu Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • How to Install OroCRM on Ubuntu 20.04
  • Ampere PMU Profiler: A Guide to Microarchitecture Profiling
  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook