Home About News Products Blog

How to display a custom maintenance page using Varnish



Here are some VCL code suitable for Varnish 4.x that will allow you to block traffic to the upsteam and display a custom maintenance page. It also whitelists specific IP addresses to allow access to the site while the rest of the public sees the maintenance page.

You can serve a custom maintenance page by replacing the HTML inside sub vcl_synth. We highly recommend keeping the HTML as small as possible and inline any CSS and JS. Any images should be hosted off domain.

If you wish to grant access to people that do not have static IP addresses, you can perhaps ask them to add a custom header in their browser. Tools like ModHeader for Chrome is great for this. So for example you can set the vcl_recv logic to be the following

sub vcl_recv {
  # If request does not have custom header, then display maintenance page. .
   if(req.http.x-bypass-maintenance != "true") {
     return (synth(800, "Maintenance page"));

Any users that wish to bypass the maintenance page will just need to add the custom request header x-bypass-maintenance with the value true in ModHeader.


If you wish to display the maintenance page for a select domain or URL path, simply add an IF condintion to the sub vcl_recv part of the code such as

if (req.http.host == "www.example.com" && req.url ~ "/example/URL/path") {