Serving Branches on a Subdomain using Caddy and GitLab Review Apps

This post describes how to work with Caddy. I've previously written about how to use GitLab Review Apps with Capistrano and Nginx, which may be of interest.

Although this example uses GitLab Review Apps, it could much as easily be used for any other branch-based deployments.

I've recently been wanting to move to Caddy, for a number of reasons, but mostly its ability to manage the Let's Encrypt Lifecycle, as well as being a single static (Go) binary.

However, one of the big concerns I had with moving from Nginx to Caddy was the configuration for my Review Apps configuration. I currently am able to publish a branch, example/review-apps, to an FQDN,, where example-review-apps is provided to me by GitLab CI in the variable CI_COMMIT_REF_SLUG.

My Nginx configuration was quite simple, utilising nginx's regular expression names:

# /etc/nginx/sites-enabled/
server {
    listen 80;
    server_name ~^(www\.)?(?<sname>.+?)$;
    root /srv/www/$sname/current/site;

    index index.html index.htm index.php;
    error_page 404 /404.html;

    charset utf-8;

    access_log /var/log/nginx/;
    error_log  /var/log/nginx/ debug;

The ease of this solution made me concerned that I would not be able to easily migrate to the same automagic redirection using Caddy. However, in Caddy 0.10.12, this was included using the concept of Caddy labels, via confirmation in the Caddy Community forums:

https://* {
  root /srv/www/
  rewrite {
    to /{label1}{uri}

Let us take the example above, and we come in on

Written by Jamie Tanna on 15 April 2018, and last updated on 30 December 2018.

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.



Other posts you may be interested in: