2

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.

Peter
  • 4,493
  • 6
  • 41
  • 64
  • Can you provide your entire caddyfile? – Toby Allen Feb 17 '19 at 11:09
  • that is my entire file – Peter Feb 17 '19 at 15:59
  • Can you provide in your question more information? Are the hosts you are redir to on the same machine? How many hosts ? Can you add a log directive to see what is being requested? WHy are you proxying all requests back to itself? What is {$PORT} what is {$API_PORT}? Any more information is always helpful – Toby Allen Feb 17 '19 at 17:47
  • Thanks @TobyAllen, I've added more information about my setup. Please let me know if I missed something out. – Peter Feb 18 '19 at 11:23
  • Can you ask your question on https://caddy.community our forum, I think you’ll get more help there. I think you should simplify until you have something working then make more complicated. There are too many unknowns in this setup and no logging – Toby Allen Feb 18 '19 at 23:23
  • We have something called 'port_in_redirect' directive in nginx which strips of the port number which doesn't cause ambiguity in application. I believe this kind of directive is missing in caddy – user3136348 Jun 07 '23 at 05:55

0 Answers0