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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Install OroCRM on Ubuntu 20.04
  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • SQL Query Performance Tuning in MySQL

Trending

  • Java Virtual Threads and Scaling
  • A Complete Guide to Modern AI Developer Tools
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • A Guide to Developing Large Language Models Part 1: Pretraining
  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 user avatar
Giorgio Sironi
·
Aug. 23, 11 · Interview
Likes (1)
Comment
Save
Tweet
Share
24.9K Views

Join the DZone community and get the full member experience.

Join For Free

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 --maintainer=youraddress@gmail.com --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
  • JMeter Plugin HTTP Simple Table Server (STS) In-Depth
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • SQL Query Performance Tuning in MySQL

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: