Serving multiple static sites?

1. The problem I’m having:

I’m serving a static website with Caddy/Docker, as follows:

In the “caddy” section of my compose.yaml file, I have defined the volumes

volumes:
 - /home/me/Docker/Caddy/Caddyfile:/etc/caddy/Caddyfile
 - /home/me/Docker/Caddy/static_site:/srv
 - caddy_data:/data
 - caddy_config:/config

And in my Caddyfile I have

mysite.net {
    root * /srv
    File_server
}

This happily serves up a static website at https://mysite.net, all files being in my server at /home/me/Docker/Caddy/static_site. But now I want to add another static website. I have plenty of DNS subdomains to use, say static.mysite.net. But I can’t find what I should add into my Caddyfile, and whether I need to add another volume in my compose.yaml file. I’ve tried adding to my Caddyfile:

    static.mysite.net {
        root * /home/me/Docker/Caddy/another_static_site
        File_server
    }

with a file new_site.html in that directory. But trying to serve that page withhttps://static.mysite.net/new_site.html doesn’t seem to work - I just get a 404 error that the web page can’t be found.

What am I missing here? Many thanks!

2. Error messages and/or full log output:

The error is simply a 404 error that the file cannot be found.

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

3. Caddy version:

Caddy version 2.7.6

4. How I installed and ran Caddy:

I’m running Caddy as a Docker container, using Docker Compose.

a. System environment:

OS is Ubuntu Server 20.04 LT.
Docker version 25.0.1
Docker compose 2.24.2

b. Command:

docker compose up - d

or

docker restart caddy

c. Service/unit/compose file:

services:
  caddy:
    container_name: caddy
    image: caddy:2.7.6
    restart: always
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
    networks:
      - caddy_net                                         
    volumes:
      - /home/me/Docker/Caddy/Caddyfile:/etc/caddy/Caddyfile
      - /home/me/Docker/Caddy/static_site:/srv            
      - caddy_data:/data
      - caddy_config:/config
      - /etc/mathesar/msar/media:/code/media
      - /etc/mathesar/msar/static:/code/static

(The last two lines are for the online database explorer Mathesar.)

d. My complete Caddy config:

This is the relevant bit

  mysite.net {
        root * /srv
        File_server
    }

And this is my attempt that doesn’t work:

testsite.mysite.net {
    root * /home/amca/Docker/Caddy/testsite
    file_server
}

5. Links to relevant resources:

Considering this is your compose.yaml

volumes:
 ...
 - /home/me/Docker/Caddy/static_site:/srv
 ...

the path /home/me/Docker/Caddy/another_static_site is outside of your container. So this:

 static.mysite.net {
     root * /home/me/Docker/Caddy/another_static_site
     File_server
 }

won’t work because that path doesn’t exist inside the container.

Assuming you have these two directories outside of your container:

/home/me/Docker/Caddy/static_site
/home/me/Docker/Caddy/another_static_site

I’d suggest changing this:

volumes:
 - /home/me/Docker/Caddy/Caddyfile:/etc/caddy/Caddyfile
 - /home/me/Docker/Caddy/static_site:/srv
 - caddy_data:/data
 - caddy_config:/config

to this:

volumes:
 - /home/me/Docker/Caddy/Caddyfile:/etc/caddy/Caddyfile
 - /home/me/Docker/Caddy:/srv
 - caddy_data:/data
 - caddy_config:/config

Notice the difference here:

 - /home/me/Docker/Caddy:/srv

Then, update your Caddyfile like this:

mysite.net {
    root * /srv/static_site
    file_server
}

testsite.mysite.net {
    root * /srv/another_static_site
    file_server
}

This way, both sites will be accessible inside the container.

2 Likes