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:
caddyserver:master
← francislavoie:file-server-status
opened 06:04AM - 20 Mar 21 UTC
After reading a question about the `handle_response` feature of `reverse_proxy`,… I realized that we didn't have a way of serving an arbitrary file with a status code other than 200. This is an issue in situations where you want to serve a custom error page in routes that are not errors, like the aforementioned `handle_response`, where you may want to retain the status code returned by the proxy but write a response with content from a file.
This feature is super simple, basically if a status code is configured (can be a status code number, or a placeholder string) then that status will be written out before serving the file - if we write the status code first, then the stdlib won't write its own (only the first HTTP status header wins).
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?
matt
(Matt Holt)
April 8, 2021, 5:24pm
4
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}
system
(system)
Closed
May 8, 2021, 4:08pm
7
This topic was automatically closed after 30 days. New replies are no longer allowed.