1. My Caddy version (caddy version
):
2.0 beta 14
2. How I run Caddy:
caddy run --config /etc/caddy/config.json
a. System environment:
CentOS 8, fresh droplet from DigitalOcean
d. My complete Caddyfile or JSON config:
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":443"
],
"routes": [
{
"match": [
{
"host": [
"sorry, cannot make this public"
]
}
],
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"encodings": {
"gzip": {},
"zstd": {}
},
"handler": "encode"
}
]
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "rewrite",
"strip_path_prefix": "/frontend/"
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [],
"root": "/var/www/html/frontend"
}
]
}
]
}
],
"match": [
{
"path": [
"/frontend*"
]
}
]
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "rewrite",
"strip_path_prefix": "/api/"
}
]
},
{
"handle": [
{
"handler": "reverse_proxy",
"headers": {
"request": {
"set": {
"Host": [
"{http.request.host}"
],
"X-Forwarded-For": [
"{http.request.remote.host}"
],
"X-Forwarded-Proto": [
"{http.request.scheme}"
],
"X-Real-Ip": [
"{http.request.remote.host}"
]
}
}
},
"health_checks": {
"active": {
"expect_status": 2,
"path": "/health"
}
},
"transport": {
"protocol": "http",
"read_buffer_size": 4096
},
"upstreams": [
{
"dial": "10.132.66.221:1325"
}
]
}
]
}
]
}
],
"match": [
{
"path": [
"/api*"
]
}
]
},
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "rewrite",
"uri": "{http.matchers.file.relative}"
}
],
"match": [
{
"file": {
"try_files": [
"{http.request.uri}",
"404.html",
"/404.html"
]
}
}
]
},
{
"handle": [
{
"handler": "file_server",
"hide": [],
"root": "/var/www/html/site"
}
]
}
]
}
]
}
]
}
],
"terminal": true
}
]
}
}
}
},
"logging": {
"sink": {
"writer": {
"output": "file",
"filename": "/var/log/caddy/std.log",
"roll": true,
"roll_size_mb": 2,
"roll_gzip": false,
"roll_local_time": false,
"roll_keep": 5,
"roll_keep_days": 10
}
},
"logs": {
"default": {
"writer": {
"output": "file",
"filename": "/var/log/caddy/caddy.log",
"roll": true,
"roll_size_mb": 2,
"roll_gzip": false,
"roll_local_time": false,
"roll_keep": 5,
"roll_keep_days": 10
}
}
}
}
}
3. The problem I’m having:
With the above configuration, i am trying to display a custom 404 page and it does not work.
I am sure it’s something easy but i just can’t find it. This is my first interaction with caddy 2.
I also need to find a way to redirect some old pages, for example, everything that comes to domain.com/{terms,privacy-policy,cookie-policy}/
must go to domain.com/legal/{terms,privacy-policy,cookie-policy}
. I tried to do this with the rewrite module, but to no avail.
How does one do a 301 redirect in caddy 2, for multiple resources?
I tried above to map some requests that hit /frontend
to another location on the server, which is different from /
and which will use a file server to serve the contents, does that look correct?
While i did try to enable logging, for some reason, the 404 errors don’t get logged, any idea why?
I also noticed JSON Config Structure - Caddy Documentation but there’s no example anywhere on how this would work, any example would help a lot.
Thanks.