Enable gzip & caching with Caddy

Overview

Recently I deployed my site to production (https://gotkey.io) and noticed that the site loaded quite slowly. When I checked with Google page speed insights, there were issues but the most significant are content not gzipped and no caching policy for the static resources.

I know how to enable such things with Nginx, httpd, and haproxy since I’ve deployed many websites using such technologies many times. For Caddy, this is my first time.

It turned out that enabling gzip and caching with Cadd is the easiest thing I’ve done.

How to enable gzip & caching with Caddy

All I needed to do was add the following config:

gotkey.io {
  @cachedFiles {
      path *.jpg *.jpeg *.png *.gif *.ico *.js *.css
  }
  header @cachedFiles Cache-Control "public, max-age=604800, must-revalidate"
  encode gzip
  reverse_proxy gotkey-fe:80
}

Specifically, from lines 2 to 5 I enabled caching for static resources. As you can see, I can specify which file types are included. If you have more static files (videos for example), you can add to this list.

Enabling gzip was done on line 6.

And that’s all!

Conclusion

This quick tutorial shows you how to enable gzip and caching with Caddy. After enabling these settings, my site’s performance improved quite significantly.

Leave a Comment