I want one rule for the ovpn.
reverse-proxy to internal ip with correct port for each port.
Some services, like openvpn, we run 3 services.
Openvpn uses 3 ports (443, 1194 and 500).
I want to route to the correct internal ip-address depending on the fqdn.
So ovpn.eltomation.com goes to 192.168.16.9, openvpn.eltomation.com goes to 192.168.16.4, and so on.
4. Error messages and/or full log output:
It isn’t working.
I get an error 502 or the wrong service or the correct service at random.
First, matchers must either be a path, starting with /, a named matcher starting with @, or * meaning “match anything”. You need to use named matchers for anything other than simple path matchers. This is explained at the top of the page you linked:
Second, It’s not really possible to match on the port. You’ll need to make two separate site blocks in this case. This is because under the hood, those are actually two separate servers, because they’re listening on different ports. You can see this by running caddy adapt --pretty to see the underlying JSON for your Caddyfile.
All of this is likely useless. Caddy automatically sets Host, X-Forwarded-For and X-Forwarded-Proto which are usually the only ones that are necessary. It also takes care to set them correctly, and to spec. Overriding them like this can cause trouble, so it’s best to let Caddy do its thing, unless you actually know you need to override them.
This isn’t always accurate; if the Host header comes in with a non-standard port, for example example.com:1234, you can use a host matcher to match on example.com:1234. I’m not sure you can (yet?) match on port alone, but maybe with wildcards? I dunno, haven’t tried it: *.*:1234
Of course, this assumes the client is virtuous… in theory, an HTTP client could connect to port A and then send a request with a Host header for port B. It’s nonsense, but I guess it could happen.
Yeah, hence the “really”. I don’t recommend it because it makes it more complicated than it needs to be, as you explained. I think just making a second site block is the better option.