Wildcard Subdomain Reverse Proxy

1. Caddy version:

2.6.4

2. How I installed, and run Caddy:

Installed using Homebrew: brew install caddy

a. System environment:

MacOS

b. Command:

caddy run

d. My complete Caddy config:

*.localhost {
    log
    reverse_proxy localhost:3000/{labels.2}
}

3. The problem I’m having:

I’m attempting to use a wildcard subdomain which reverse proxies to another domain with the subdomain value being appended to the path. For example: about.localhostlocalhost:3000/about

However, Caddy returns the following error: invalid dial address localhost:3000/: missing port in address

cURL output

curl -Lkv https://about.localhost
*   Trying 127.0.0.1:443...
* Connected to about.localhost (127.0.0.1) port 443 (#0)
* ALPN: offers h2
* ALPN: offers http/1.1
* (304) (OUT), TLS handshake, Client hello (1):
* (304) (IN), TLS handshake, Server hello (2):
* (304) (IN), TLS handshake, Unknown (8):
* (304) (IN), TLS handshake, Certificate (11):
* (304) (IN), TLS handshake, CERT verify (15):
* (304) (IN), TLS handshake, Finished (20):
* (304) (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / AEAD-CHACHA20-POLY1305-SHA256
* ALPN: server accepted h2
* Server certificate:
*  subject: [NONE]
*  start date: Feb 15 12:09:08 2023 GMT
*  expire date: Feb 16 00:09:08 2023 GMT
*  issuer: CN=Caddy Local Authority - ECC Intermediate
*  SSL certificate verify ok.
* Using HTTP2, server supports multiplexing
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* h2h3 [:method: GET]
* h2h3 [:path: /]
* h2h3 [:scheme: https]
* h2h3 [:authority: about.localhost]
* h2h3 [user-agent: curl/7.85.0]
* h2h3 [accept: */*]
* Using Stream ID: 1 (easy handle 0x126014800)
> GET / HTTP/2
> Host: about.localhost
> user-agent: curl/7.85.0
> accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 502 
< alt-svc: h3=":443"; ma=2592000
< server: Caddy
< content-length: 0
< date: Wed, 15 Feb 2023 13:17:10 GMT
< 
* Connection #0 to host about.localhost left intact

4. Error messages and/or full log output:

Pretty printed the JSON for readability

http.log.error  making dial info: upstream localhost:3000/{http.request.host.labels.2}: invalid dial address localhost:3000/: missing port in address   
{
  "request": {
    "remote_ip": "127.0.0.1",
    "remote_port": "49293",
    "proto": "HTTP/2.0",
    "method": "GET",
    "host": "about.localhost",
    "uri": "/",
    "headers": {
      "User-Agent": ["curl/7.85.0"],
      "Accept": ["*/*"]
    },
    "tls": {
      "resumed": false,
      "version": 772,
      "cipher_suite": 4867,
      "proto": "h2",
      "server_name": "about.localhost"
    }
  },
  "duration": 0.000355125,
  "status": 502,
  "err_id": "8xu9c7kk6",
  "err_trace": "reverseproxy.statusError (reverseproxy.go:1299)"
}

5. What I already tried:

I’ve attempted using the rewrite directive (don’t think this is what I need, but I’m not 100%) and using the answer posted in this other help article however neither have solved my issue.

6. Links to relevant resources:

Yes, it is what you need.

Try this:

*.localhost {
	log
	rewrite * /{labels.2}{uri}
	reverse_proxy localhost:3000
}

This will rewrite https://foo.localhost/bar to the request path /foo/bar, then proxy it to localhost:3000.

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