DEV Community

Marco Kamner
Marco Kamner

Posted on • Originally published at blog.marco.ninja

Caddy: Manual Maintenance Mode

Coming from NGINX and others the concept of a maintenance mode that can be manually enabled is something I have used many times before.

With Caddy it is equally as easy, just using a less obvious syntax.

It always follows this general pattern:

  • Check if a maintenance.on file exists at a specified location
  • If it does, deliver the maintenance page
  • Otherwise, do the normal thing you do

Notice:
Doing maintenance pages the way I describe in this post means your webserver is checking a file on disk for every request.

In small deployments this is likely not adding any overhead you need to be concerned with, but keep it in mind.

nginx

To refresh your memory, this patterns looks like this in nginx.

server {
  location / {
    set $maintenance 0;
    if (-f /app/maintenance.on) {
       set $maintenance 1;
    }
    if ($maintenance = 1) {
      rewrite ^(.*)$ /app/static/maintenance.html last;
      return 503;
    }

    proxy_pass http://127.0.0.1:8080;
  }
}
Enter fullscreen mode Exit fullscreen mode

Caddyfile

With Caddy it works exactly the same, just using a different vocabulary.

example.marco.ninja {
    @maintenanceModeActive file /app/maintenance.on
    handle @maintenanceModeActive {
        try_files /app/static/maintenance.html
        file_server {
            status 503
        }
    }

    reverse_proxy 127.0.0.1:8080
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)