Hey everyone! So I have a setup with Docker Composer and Coolify. In that Docker container, I have my caddy container.
Setup: Caddy(coolify) → [MyCaddy(docker) → backend+frontend(docker)]
MyCaddy file is very simple; it just chooses when to redirect to the backend or to the frontend depending on the presence of ‘api’ in the URI.
My caddy does not provide https; that is handled by Coolify’s first caddy proxy.
This setup works fine in production with non-www.
You can skip needing to do this if you configure trusted_proxies so that Caddy recognizes that the request comes from a trusted server, and to retain the original X-Forwarded-* headers.
I don’t really understand the question then. Why not follow that pattern? Shouldn’t you do that redirect on the Caddy server that handles TLS/certs? That makes the most sense, rather than trying to redirect on the deeper one which is closer to your app.
Regarding the X-Forwarded-Proto, this is on my to-do list for sure, but it does not matter for this issue (I think). Sorry, I probably should have removed this part from the question to not distract from the problem at hand.
The question is:
Given my caddy file, how can I redirect www. requests to non-www, keeping in mind that my caddy file is defined as:
The following is merely illustrative of what I am trying to accomplish; it does not work or exist:
# Bellow does not work
www* {
redir https://{host}{uri}
}
:80 {
handle /api/* {
...
}
handle {
...
}
}
Why is it like this?
R: Versatality
I like having all my logic and communication between servers contained in a single docker-compose and network. This way, if I need to move my app elsewhere, it is easier; just grab the docker-compos file. I also like having a single entrypoint (my caddy) to worry about/configure and know that from that point on everything should work just like in local development.
Having it on port 80 and taking advantage of the docker-compose profiles feature, I can easily swap between development and production environments; I just need to docker compose --prodile (prod or dev) up -d and be good to go. Having it work with ‘localhost’ and ‘example.com’ by just changing a word in the command.
I want to keep the Coolify part as simple as possible so not to mess with its proxy (Caddy). If I desire to change it to something else like Versel or Dokploy (I think, not sure if they provide this), I will just grab the docker-compose file and make sure that my domain points to ‘my caddy’ deployed there. I do not want to have to learn about how to configure their proxies (let’s imagine the other uses Traefik or Nginx).
As it is currently in Coolify, I just need to present my docker-compose file and tell that ‘example. com’ points to the ‘my caddy’ container.
Right, but you still need a certificate for it to work, and since you have a front instance of Caddy, you should do it in the same server that owns the certificate. It’s weird to proxy down to only get served a redirect.
Hey there! Sorry for the late reply; I had to step away for a while. Just tested this and it works perfectly. Thank you so much for helping a stranger.