308 redirect when redirect http to https

1. The problem I’m having:

I’m using caddy server for a callback listener installation that i have.
Callbacks are essentially payments updates.
I’m able to receive the callback but the system in stuck in an infinite redirect loop and doesn’t hit my server located at localhost:3000.
i can’t only have https request made to my callback listener url because the payment company only send http request on sandbox

2. Error messages and/or full log output:

{"level":"info","ts":1725613155.9954963,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"52.236.59.28","remote_port":"39804","client_ip":"52.236.59.28","proto":"HTTP/1.1","method":"POST","host":"momo.boursenumeriquedafrique.com:80","uri":"/mtn","headers":{"Content-Length":["234"],"Connection":["Keep-Alive"],"User-Agent":["LWAC Http Client 1.0"],"Accept-Encoding":["gzip,deflate"],"Content-Type":["application/json; charset=utf-8"]}},"bytes_read":0,"user_id":"","duration":0.00083802,"size":0,"status":308,"resp_headers":{"Server":["Caddy"],"Connection":["close"],"Location":["https://momo.boursenumeriquedafrique.com/mtn"],"Content-Type":[]}}
{"level":"info","ts":1725613157.0462492,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"52.236.59.28","remote_port":"39806","client_ip":"52.236.59.28","proto":"HTTP/1.1","method":"POST","host":"momo.boursenumeriquedafrique.com:80","uri":"/mtn","headers":{"Content-Type":["application/json; charset=utf-8"],"Content-Length":["234"],"Connection":["Keep-Alive"],"User-Agent":["LWAC Http Client 1.0"],"Accept-Encoding":["gzip,deflate"]}},"bytes_read":0,"user_id":"","duration":0.000604014,"size":0,"status":308,"resp_headers":{"Server":["Caddy"],"Connection":["close"],"Location":["https://momo.boursenumeriquedafrique.com/mtn"],"Content-Type":[]}}
{"level":"info","ts":1725613217.1610556,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"52.236.59.28","remote_port":"40054","client_ip":"52.236.59.28","proto":"HTTP/1.1","method":"POST","host":"momo.boursenumeriquedafrique.com:80","uri":"/mtn","headers":{"Accept-Encoding":["gzip,deflate"],"Content-Type":["application/json; charset=utf-8"],"Content-Length":["234"],"Connection":["Keep-Alive"],"User-Agent":["LWAC Http Client 1.0"]}},"bytes_read":0,"user_id":"","duration":0.000620015,"size":0,"status":308,"resp_headers":{"Server":["Caddy"],"Connection":["close"],"Location":["https://momo.boursenumeriquedafrique.com/mtn"],"Content-Type":[]}}
{"level":"info","ts":1725613277.2180464,"logger":"http.log.access.log0","msg":"handled request","request":{"remote_ip":"52.236.59.28","remote_port":"40238","client_ip":"52.236.59.28","proto":"HTTP/1.1","method":"POST","host":"momo.boursenumeriquedafrique.com:80","uri":"/mtn","headers":{"Content-Type":["application/json; charset=utf-8"],"Content-Length":["234"],"Connection":["Keep-Alive"],"User-Agent":["LWAC Http Client 1.0"],"Accept-Encoding":["gzip,deflate"]}},"bytes_read":0,"user_id":"","duration":0.000422343,"size":0,"status":308,"resp_headers":{"Server":["Caddy"],"Connection":["close"],"Location":["https://momo.boursenumeriquedafrique.com/mtn"],"Content-Type":[]}}

3. Caddy version:

v2.8.4 h1:q3pe0wpBj1OcHFZ3n/1nl4V4bxBrYoSoab7rL9BMYNk=

4. How I installed and ran Caddy:

a. System environment:

Ubuntu 24.04 LTS , RISCV 64

b. Command:

sudo systemctl restart caddy

c. Service/unit/compose file:

d. My complete Caddy config:

momo.boursenumeriquedafrique.com {
        tls {
                issuer zerossl pdf {
                        dns_ttl 10m20s
                }
        }


        # Redirect http to https
        @http {
                protocol http
        }

        redir @http https://{host}{uri} permanent

        @allowed_methods {
                method POST PUT
        }
        reverse_proxy @allowed_methods localhost:3000

        @disallowed_methods {
                not method POST PUT
        }

        respond @disallowed_methods 405

        log {
                output file /var/log/caddy/boursenumerique.log
                format json
        }
}

5. Links to relevant resources:

The logs you’ve shared don’t show a loop. They’re all on port :80 with a redirect to https only.

Why this? You’ve configured Caddy with a domain name, for which Caddy will automatically offer HTTPS with an automatic redirect from :80 (http) to :443 (https)

1 Like

Hi Mohammed90 , thank you for taking the time to review my case.
The problem is , even if i remove the redirection directive because it’s automatically handled by Caddy, it’s still the same.
Requests never reach my localhost:3000 port

Are you saying a payment company doesn’t handle HTTPS?? Throw them out and into the bin!

Although extremely not recommended, you can turn the http-to-https off and manage it manually by making 2 duplicated blocks of your site, one with port :80 and another without (implies :443).

2 Likes

Mohammed90 It’s an African company , unfortunately, the leader of the market. They do https but only in production not in sandbox.

Working solution.

momo.boursenumeriquedafrique.com,  momo.boursenumeriquedafrique.com:80 {
        tls {
                issuer zerossl pdf {
                        dns_ttl 10m20s
                }
        }

        @allowed_methods {
                method POST PUT
        }
        reverse_proxy @allowed_methods localhost:3000

        @disallowed_methods {
                not method POST PUT
        }

        respond @disallowed_methods 405

        log {
                output file /var/log/caddy/boursenumerique.log
                format json
        }
}

They make the request especifically to the port 80 of the callback url i provide to them (momo.boursenumeriquedafrique.com).

i was not receiving anything because i didn’t handled this case, adding momo.boursenumeriquedafrique.com:80 make it work. in production they will probably send requests to url:443 … maybe i will be looking for a better solution.

In any case thank you very much with the guidance Mohammed90

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.