Caddy as a multi-page website router

1. Caddy version (caddy version):

2.4.3

2. How I run Caddy:

I have this Dockerfile I through together. I’m new at Docker, so also curious if there is any improvements to be had here but not relevant to my post:

FROM alpine:3.12.2

ARG VERSION="0.10.11"

RUN apk update && apk upgrade \
  && apk add --no-cache openssh-client git \
  && apk add --no-cache --virtual .build-dependencies tar curl

RUN curl --silent --show-error --fail --location \
      --header "Accept: application/tar+gzip, application/x-gzip, application/octet-stream" -o - \
      "https://github.com/caddyserver/caddy/releases/download/v2.4.3/caddy_2.4.3_linux_arm64.tar.gz" \
    | tar --no-same-owner -C /usr/bin -xz \
 && chmod 0755 /usr/bin/caddy \
 && /usr/bin/caddy version \
 && apk del .build-dependencies

EXPOSE 80 443 2019

# No idea what this does but saw some people with it
VOLUME /root/.caddy

COPY caddy.json /etc/caddy.json

# Put all HTML / CSS / IMAGES in this dir
WORKDIR /dist
COPY . /dist/

ENTRYPOINT ["/usr/bin/caddy"]
CMD ["run", "-config", "/etc/caddy.json", "-resume"]
# to build: docker build -t caddy-project .
# to run: docker run -d --name caddy -p 80:80 -p 443:443 -p 2019:2019 caddy-project run 

a. System environment:

Docker, possibly a Kubernetes cluster later.
Docker 20.10.2

b. Command:

docker build -t caddy-project .
docker run -d --name caddy -p 80:80 -p 443:443 -p 2019:2019 caddy-project run 

3. The problem I’m having:

I want to know if Caddy can be used as a router between pages and if that is even a good use-case for Caddy. For example, let’s say my website has multiple pages:
www.example.com
www.example.com/blog/
www.example.com/blog/article-july-20
www.example.com/about
www.example.com/contact

Can I use Caddy to basically do this routing? If so, what would be the proper way to do this?

If not, I could take a different approach using Vue Router and trying to make an SPA as well, so I’d also be looking for suggestions on getting that started.

4. Error messages and/or full log output:

None

5. What I already tried:

I haven’t actually thrown together a protetype yet. Sorry! I just want to first gage if using Caddy this way is even possible.

I’m still pretty new as well but I’ll try to write what I think would be a good Caddyfile for such a thing:

www.example.com {
	redir https://example.com{uri}
}

example.com {
    root * /path/to/index.html
    tls /path/to/server.cert /path/to/server.key # map a host to container volume if you want to use certificates stored on the host
    reverse_proxy /api/v1/* localhost:5000
    file_server
}

example.com/about {
    root * /path/to/about.html
    tls /path/to/server.cert /path/to/server.key # map a host to container volume if you want to use certificates stored on the host
    reverse_proxy /api/v1/* localhost:5000
    file_server
}

<repeat this pattern for every page?>

Thanks for your help!

There’s an official Docker image for Caddy. Use that instead. Read the docs on Docker to understand how to use it.

Consider using docker-compose as well, it’s an easier to use interface to running Docker, especially for beginners.

If those are all HTML files, then yeah, you just need to use the try_files directive to tell Caddy to try to rewrite the request to the file by appending .html to the end of the request path. The Caddyfile might look like this:

example.com {
	handle /api/v1* {
		reverse_proxy api:5000
	}

	handle {
		root * /srv
		try_files {path}.html {path}
		file_server
	}
}

So basically, all requests to /api/v1* will be handled by your other container in the same docker network named api on port 5000, and the rest will be attempted to be served with Caddy’s file server (which you can mount with a volume, or COPY in your Dockerfile at /srv), first trying to append .html to the end of the request path (so if the request is /about, it will try /about.html first, and it that doesn’t work it just falls back to /about which might end in a 404 response if no such file exists).

You shouldn’t need this line, Caddy can manage TLS certificates for you automatically. In fact, it’s essentially Caddy’s headlining feature.

2 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.