Entry with no port specified redirects to wrong port entry

1. Caddy version (caddy version):

2.4.6

2. How I run Caddy:

Via docker compose con Amazon ECS.

a. System environment:

Amazon ECS, Fargate. Docker compose file with docker client using ECS context.

b. Command:

Caddyfile is base64 encoded in and environment variable. Then is decoded and copied to /etc/caddy/Caddyfile. Then caddy run --config /etc/caddy/Caddyfile --adapter caddyfile.

c. Service/unit/compose file:

services:
  caddy:
    image: caddy:2.4.6-alpine
    ports:
      - "80:80"
      - "443:443"
      - "2345:2345"
    volumes:
      - caddy_data:/data
      - caddy_config:/config
    command: ["sh", "-c", "echo ${CADDYFILE_B64} | base64 -d > /etc/caddy/Caddyfile && caddy run --config /etc/caddy/Caddyfile --adapter caddyfile"]
volumes:
  caddy_data:
  caddy_config:

d. My complete Caddyfile or JSON config:

{
    debug
}

https://example.example.com {
    reverse_proxy http://localhost:1234
}

https://example.example.com:2345 {
    reverse_proxy http://localhost:2345
}

3. The problem I’m having:

When we do go to http://example.example.com/, we get redirected to https://example.example.com:2345/ instead of https://example.example.com:1234/.

% curl -IL http://example.example.com                                                                                          
HTTP/1.1 308 Permanent Redirect
Connection: close
Location: https://example.example.com:2345/
Server: Caddy
Date: Wed, 12 Jan 2022 14:57:00 GMT

HTTP/2 200 
content-type: application/octet-stream
date: Wed, 12 Jan 2022 14:57:00 GMT
server: Caddy
content-length: 0

4. Error messages and/or full log output:

5. What I already tried:

Tried changing the order of the entries.

1 Like

Hmm, weird. I guess we haven’t taken the port number into account when it comes to building the HTTP->HTTPS redirects.

I’d suggest using another subdomain for that other site though. Is there a specific reason you need to use a different port?

But yeah, I’ll look into this soon. If you want to open up a bug report on github for this, it’d be appreciated :+1:

1 Like

That is a weird case I’m not sure I tested for, there’s two HTTPS sites for that domain, one is on the HTTPS port (443) and another is on a random port (2345).

We do consider the port number in general, but I wonder if in this case it just chose the wrong one.

Thank you for your help!

Issue open here.

1 Like

And @francislavoie has already fixed it :smile:

2 Likes