My web host already provides SSL configured on my domain so I want to use Caddy server to redirect HTTP traffic to HTTPS using the "Redirect only if the forwarded protocol is HTTP" example from the docs. The issue is that the browser is unable to render the page my caddy config. Going to the URL shows the error:
The page isn't redirecting properly. An error occurred during a connection to ****. This problem can sometimes be caused by disabling or refusing to accept cookies. (Firefox)
This page isn’t working **** redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS (Chrome)
This is my Caddyfile
0.0.0.0:{$PORT} {
bind {$ADDRESS}
proxy / 0.0.0.0:{$API_PORT}
redir 301 {
if {>X-Forwarded-Proto} is http
/ https://{host}{uri}
}
}
My host provides $PORT=8080 as the main entry point for web traffic so I started my NodeJS app to listen on $API_PORT=9090. My expectation is that Caddy receives all traffic on port 0.0.0.0:8080
and redirects all non-HTTPS requests to my NodeJS app on port 0.0.0.0:9090
. I like this idea because I would prefer not to put networking logic in the app but maybe I'm wrong about that.
Caddy and NodeJS are on the same machine.