Serving Branches on a Subdomain using Caddy and GitLab Review Apps

Featured image for sharing metadata for article

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

  • Caddy passes the value of the wildcard into label1, which with the rewrite rule expands out to /example-review-apps/test
  • Caddy combines this with the root directive to expand this out on disk to /srv/www/

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

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.

#guide #gitlab #review-apps #caddy #howto #deploy.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.