Note: This article was originally published in 2013. Some steps, commands, or software versions may have changed. Check the current Varnish documentation for the latest information.
In this step-by-step guide, you’ll learn not serve cached pages to logged in users using varnish.
How to: Not serve cached pages to logged in users using (http://www.varnish-cache.org/ “Varnish (software)”)
Generally speaking caching is a good thing, it takes a bit load off the web servers and serves the content more quickly to our site visitors. However, there are a few scenarios where caching is not that great: A highly dynamic site for example would not like the new content being published from not being show to clients. Fortunately there is a solution for many of the problems out there: You can purge the cache smartly or simply serve directly (no caching) portions of your site based on different criteria.
Varnish is an (http://en.wikipedia.org/wiki/HTTP_accelerator “HTTP accelerator”) designed for content-heavy (http://en.wikipedia.org/wiki/Dynamic_web_site “Dynamic web site”). In contrast to other (http://en.wikipedia.org/wiki/Web_accelerator “Web accelerator”), such as (http://en.wikipedia.org/wiki/Squid_\(software\) “Squid (software)”), which began life as a client-side cache, or (http://en.wikipedia.org/wiki/Apache_HTTP_server “Apache HTTP server”) and (http://en.wikipedia.org/wiki/Nginx “Nginx”), which are primarily origin servers, Varnish was designed as an HTTP accelerator. Varnish is focused exclusively on (http://en.wikipedia.org/wiki/HTTP “HTTP”), unlike other (http://en.wikipedia.org/wiki/Proxy_server “Proxy server”) that often support for (http://en.wikipedia.org/wiki/FTP “FTP”), (http://en.wikipedia.org/wiki/SMTP “SMTP”) and other (http://en.wikipedia.org/wiki/Network_protocol “Network protocol”).
There are many solutions out there for caching, from the built in cache in (http://nginx.org/ “Nginx”) to Varnish which is a very popular reverse proxy. I have already set up purging to work with Varnish so that when new content is published it is served to the site visitors but logged in users were experiencing issues. For example: Varnish would serve a cached page to the visitor instead of a personalize done based on his/her session. So take for example a (http://wordpress.org “WordPress”) site. An administrator might be logged in but on screen you would not be able to see the admin bar and might experience some session issues. The clearest one would be if your site used SSL as Varnish does not support SSL. So now regular HTTP requests are handled by Varnish while your web server handles HTTPS
Varnish offers a few options, some of which are: 1) Create a cache for each user 2) Do not cache logged in users. In my case because there number of users is super small I have no issues serving them content straight from the server. In order to achieve this you need to edit the VCL file used by Varnish and add the following inside sub vcl_fetch :
if (req.http.Cookie ~ “(UserID|_session)”)
{
set beresp.http.X-Cacheable = “NO:Got Session”;
return(pass);
}
For more Varnish rules, check out the following posts:
(http://img.zemanta.com/zemified_h.png?x-id=6c6876dd-3784-42e3-a78f-8fbe6cdff88e)](http://www.zemanta.com/?px “Enhanced by Zemanta”)
Summary
You’ve successfully learned not serve cached pages to logged in users using varnish. If you run into any issues, double-check the prerequisites and ensure your Varnish environment is properly configured.