1. The problem I’m having:
I’m using snippets in the Caddyfile in order to reuse as much configuration content as possible and to avoid copying&pasting and producing errors due to gettings those copy&paste clones out of sync.
Now I am wondering whether I can do conditionals within snippets.
Consider a Caddyfile
as follows:
(logging) {
log {
format transform "{common_log}" ### how can I make this conditional?
output file /var/log/caddy/{args[0]}.log {
roll_size 10mb
roll_keep 5
}
}
}
(common) {
header {
# Enable headers for all sites here ...
}
import logging {args[0]}
}
(common_proxy_https) {
reverse_proxy {args[1]} {
transport http {
tls_server_name {host}
}
}
import common {args[0]}
}
# Global options
{
import logging caddy
events {
on cert_obtained exec ...
}
}
# Reverse proxies
host1.example.tld {
import common_proxy_https service1 https://service1.local
}
host2.example.tld {
import common_proxy_https service2 https://service2.local
}
host3.example.tld {
import common_proxy_https service3 https://service3.local
}
I would like to keep JSON log format for the main caddy.log
(via global options import logging caddy
) but for the actual reverse proxies I would like to have traditional Apache access log format in order for fail2ban to work.
Therefore my question: Is it possible to have a conditional on {args[0]} != "caddy"
within snippet (logging)
so that the format transform "{common_log}"
is only active if it is not the main caddy log?
2. Error messages and/or full log output:
No error messages, I’d like to improve Caddyfile
configuration.
3. Caddy version:
$ caddy version
v2.7.4 h1:J8nisjdOxnYHXlorUKXY75Gr6iBfudfoGhrJ8t7/flI=
4. How I installed and ran Caddy:
a. System environment:
x86_64 Debian GNU/Linux
b. Command:
I diverted custom caddy according to the documentation at Build from source — Caddy Documentation
c. Service/unit/compose file:
n/a
d. My complete Caddy config:
See above.
5. Links to relevant resources:
n/a