Serving a single static file that matches multiple paths with a HTTP Status Code of 410

1. Caddy version (caddy version):

2.3.0-alpine

2. How I run Caddy:

Ran through Docker as the entry point into port 443.

a. System environment:

Docker

b. Command:

paste command here

c. Service/unit/compose file:

services:
  caddy:
    container_name: caddy
    image: caddy
    ports:
      - "80:80"
      - "443:443"
      - "80:80/udp"
      - "443:443/udp"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    environment:
      - DOMAIN=https://mydomainname.com
    restart: always

d. My complete Caddyfile or JSON config:

{$DOMAIN}/forum/* {
    respond 410
    # For any path under forum, 
    # I'd like to serve a static HTML file here that's under /static/moved.html
}

3. The problem I’m having:

I would like to mount a static folder containing HTML files and serve specific html files to specific routes.

For any path under /forum, I want to serve a static file with a 401 status code.
I can use file_server but that wouldn’t necessarily serve that same file for every single path.

I think you’ll need this PR for this:

Also FWIW, I don’t recommend using path matchers in site addresses. I recommend writing your config like this instead:

{$DOMAIN} {
	handle /forum/* {
		root * /static
		rewrite * moved.html
		file_server {
			status 410
		}
	}

	handle {
		# Fallback for otherwise unhandled requests
	}
}

Thanks. That’s really useful!

As an alternative, while I wait for that to hit a Caddy release, can I instead somehow load that HTML into a variable and serve it via a traditional respond “HTML_CONTENT_HERE” 410?

You can use respond in the meantime:

respond 410 {
    body `HTML here`
}

Right, I understand that but is there any way I can manually load the html from the file into a variable or something in order to output it to body?

If you can get it into an environment variable then you can do {env.YOUR_HTML} or {$YOUR_HTML}

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