caddy: Caddy is not matching hostname when calling with IP + Host header (ERROR: no certificate available for IP)

Needs

I need to access my home services behind a CGNAT. To solve that, I have the following:

flowchart TD
  aa(remote user) -.->|https://sub.domain.tld| a
  bb(home user) -.->|https://sub.domain.tld| d
subgraph VPS
  a(Caddy server) -->|http| b(tunnel server)
end
  b ===|http tunnel| c(tunnel client)
subgraph home server behind NAT
  c -->|http| d(Caddy server)
  d -->|http| e(app)
end

Problem

When the remote user requests https://sub.domain.tld to the VPS, then I have a blank screen (e.g. no match) 🏳️

In parallel, when the home user requests https://sub.domain.tld directly to the home server (thanks to custom home dns), then I have the web app displayed ✅

If I replace the reverse_proxy entry sub.domain.tld by :80, then the remote user is able to reach it 🤯

It feels like there is an issue with some domain name matcher.

Reproduction

I’m able to reproduce with just 2 caddy instances on the same host and same network:

flowchart LR
  user -.->|https://sub.domain.tld\nblank answer| a
  user -.->|https://sub.domain.tld\nworking| b
  a(Caddy server A) -->|http| b(Caddy server B)
  b -->|http| c(app)

The app used is mendhak/http-https-echo to retrieve which headers have been passed to the app (especially for X-Forwarded-<..>). After, I also added the :80 matcher as a fallback and then the app responded well when calling Caddy server A. I noticed that the only difference between calling server A (fallbacking on server B through :80) or server B (through http://sub.domain.tld) is only X-Forward-Proto that is respectively http and https. I added headers_up X-Forward-Proto https to server B that fixes the difference, but still going through :80.

Here the caddy configs:

# Caddy server A
http://sub.domain.tld {
        reverse_proxy http://<ip of Caddy server B>:80
}

# Caddy server B
{
        servers {
              trusted_proxies static <server A network ip>/24
        }
}
http://sub.domain.tld {
        reverse_proxy http://<ip of app>:<port of app>
}
:80 {
        reverse_proxy http://<ip of app>:<port of app>
}

EDIT: added trusted_proxies

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (10 by maintainers)

Most upvoted comments

auto_https disable_redirects doesn’t turn off HTTPS, it only removes the HTTP->HTTPS redirect when you make HTTP requests. Caddy is still listening for HTTPS requests with ping.nas.mydomain.tld. If you want to handle HTTP requests (not HTTPS) then prefix the site address with http:// (such as http://ping.nas.mydomain.tld).

It feels like the SNI is loosen somewhere… I opened a ticket on the frp side fatedier/frp#3915

I found this in frp code after doing a quick search, which seems relevant:

https://github.com/fatedier/frp/blob/dev/pkg%2Fconfig%2Fv1%2Fcommon.go#L94-L96

I’ll come back tomorrow for the other bit.