My perfect caddy setup
So i wanted to setup serverside logging for my selfhosted applications to get some insights into what is used the most and how much bandwidth is being spent etc, i quickly found that goaccess is the best tool to get these insights.
I found the example from hectorm to be the main way to get started but it was missing some things, it didn’t log the hostname used and didn’t work with proxies like Cloudflare it was also quite boilerplate full to add more domains.
So i modified the config to add these features
This is already much better but i needed to get the real ip in my logs so i didn’t just see Cloudflare ips, this can be done in 2 ways:
- Manually read the headers for the x-forwarded-for header
{request>headers>X-Forwarded-For>[0]:request>headers>Cf-Connecting-Ip>[0]:request>remote_ip}
- Add cloudflare to trusted proxies theres a module for that https://github.com/WeidiDeng/caddy-cloudflare-ip then just add the cloudflare ip to trusted proxies
But if you are like me and use cloudflare tunnel (cloudflared) you have to configure it to use using the trusted_proxies static private_ranges
option https://caddyserver.com/docs/caddyfile/options#trusted-proxies.
Although i couldn’t get that method to work with the logger for some reason im guessing its cause of the usage of request>remote_ip
instead of client_ip but i haven’t tried using client_ip yet.
After that i also wanted to get ssl certs for my domains using a letsencrypt and the cloudflare api. I did this by using https://github.com/caddy-dns/cloudflare and updated my config to use the api key
Now the ssl certs just work out of the box!, i can use caddy for internal and external use on the same server.
As a added bonus i configured Cloudflareds fallback to be localhost:80 and prefixed all my externals domains with http:// and disabled https redirects and cloudflared worked too now i just need to add a dns record pointing to the tunnel and update my caddyfile to add a new service.
My final config:
final docker-compose file
I also made my own caddy Dockerfile to use with it
As you might see i changed the port from goaccess to 443 this is cause the port is hardcoded based on your config. Here is my final goaccess config too
I put my config in /etc/goaccess/goaccess.conf
cause it was easier for testing but feel free to keep it as in the hectorm’s example