Repository: caddyserver/caddy (language: go)

Issue:
The `reverse-proxy` does not work when `--from https://domain:port`
It still tries to listen on `:80`

```
# ./caddy-2 reverse-proxy --from https://localhost:2096 --to http://127.0.0.1:8880 --internal-certs
2023/01/07 02:58:07.806	WARN	admin	admin endpoint disabled
2023/01/07 02:58:07.807	INFO	http	enabling automatic HTTP->HTTPS redirects	{"server_name": "proxy"}
2023/01/07 02:58:07.808	INFO	tls.cache.maintenance	started background certificate maintenance	{"cache": "0xc000570f50"}
2023/01/07 02:58:07.833	INFO	pki.ca.local	root certificate is already trusted by system	{"path": "storage:pki/authorities/local/root.crt"}
2023/01/07 02:58:07.834	INFO	http	enabling HTTP/3 listener	{"addr": ":2096"}
2023/01/07 02:58:07.835	INFO	failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details.
2023/01/07 02:58:07.835	INFO	http.log	server running	{"name": "proxy", "protocols": ["h1", "h2", "h3"]}
2023/01/07 02:58:07.835	INFO	tls.cache.maintenance	stopped background certificate maintenance	{"cache": "0xc000570f50"}
Error: loading new config: http app module: start: listening on :80: listen tcp :80: bind: address already in use

# ./caddy-2 version
v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=
```


Discussion / hints:
That's normal, Caddy listens on port 80 to perform HTTP->HTTPS redirects and to solve ACME HTTP challenges.
> Caddy listens on port 80 to perform HTTP->HTTPS redirects

I have explicitly passed options just to proxy traffic from `https://localhost:2096` to `http://127.0.0.1:8880`.
Since `2096` is not `443` I'd expect Caddy to listen on `2096` only.

The requirement of listening on port `80` is not desired as there is another service running on that port.

> to solve ACME HTTP challenges

Note the `--internal-certs` option. I think it should use a self-signed certificate here, so there should be no need for ACME.
This is expected behavior and I think, this issue should be close.
Just seeing this...

Port 80 is still used for http redirects, as currently most browsers don't default to https. Especially since it's on a non standard port in your case. The URL with https:// scheme tells Caddy to serve https on the non-standard port (which it would do by default anyway).

You can achieve the desired behavior with a simple Caddyfile though. I'm mobile right now but I can post an example soon.
Appreciate your feedback! That's true that browsers don't default to HTTPS yet. However given it's a non standard port, it wouldn't change anything if they did.

I understand that using a Caddyfile fixes the issue. Using a command is more convenient in my case. I think an option to disable the :80 server would be even better, something like `--no-http`.
Yeah, we can add a `--disable-redirects` flag to set it to true here in the config https://caddyserver.com/docs/json/apps/http/servers/automatic_https/

Fix patch:
diff --git a/modules/caddyhttp/reverseproxy/command.go b/modules/caddyhttp/reverseproxy/command.go
index ca62191040a..44f4c22245a 100644
--- a/modules/caddyhttp/reverseproxy/command.go
+++ b/modules/caddyhttp/reverseproxy/command.go
@@ -64,6 +64,7 @@ default, all incoming headers are passed through unmodified.)
 			fs.Bool("insecure", false, "Disable TLS verification (WARNING: DISABLES SECURITY BY NOT VERIFYING SSL CERTIFICATES!)")
 			fs.Bool("internal-certs", false, "Use internal CA for issuing certs")
 			fs.Bool("debug", false, "Enable verbose debug logs")
+			fs.Bool("disable-redirects", false, "Disable HTTP->HTTPS redirects")
 			return fs
 		}(),
 	})
@@ -77,6 +78,7 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
 	insecure := fs.Bool("insecure")
 	internalCerts := fs.Bool("internal-certs")
 	debug := fs.Bool("debug")
+	disableRedir := fs.Bool("disable-redirects")
 
 	httpPort := strconv.Itoa(caddyhttp.DefaultHTTPPort)
 	httpsPort := strconv.Itoa(caddyhttp.DefaultHTTPSPort)
@@ -174,6 +176,8 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
 
 	if fromAddr.Scheme == "http" {
 		server.AutoHTTPS = &caddyhttp.AutoHTTPSConfig{Disabled: true}
+	} else if disableRedir {
+		server.AutoHTTPS = &caddyhttp.AutoHTTPSConfig{DisableRedir: true}
 	}
 
 	httpApp := caddyhttp.App{


Test patch: