Bad request (400) when using caddy as reverse proxy

1. The problem I’m having:

I used caddy as reverse proxy on my windows server, proxying request to the local IIS, however caddy returns Bad Request - Invalid Hostname, which I can’t figure out why

2. Error messages and/or full log output:

proto": "HTTP/1.1", "method": "GET", "host": "****.com:9001", "uri": "/favicon.ico", "headers": {"X-Forwarded-For": ["77.77.114.108"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"], "Referer": ["http://****.com:9001/"], "X-Forwarded-Host": ["****.com:9001"], "Accept-Language": ["en-US,en;q=0.9,fa;q=0.8"], "Dnt": ["1"], "Accept": ["image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"], "Accept-Encoding": ["gzip, deflate"], "X-Forwarded-Proto": ["http"]}}, "headers": {"Content-Type": ["text/html; charset=us-ascii"], "Server": ["Microsoft-HTTPAPI/2.0"], "Date": ["Fri, 15 Nov 2024 12:37:34 GMT"], "Content-Length": ["334"]}, "status": 400}
2024/11/15 12:39:43.446 DEBUG   events  event   {"name": "tls_get_certificate", "id": "8f874bdd-0575-4b61-8930-86b4d927c43d", "origin": "tls", "data": {"client_hello":{"CipherSuites":[49196,49195,49200,49199,159,158,49188,49187,49192,49191,49162,49161,49172,49171,157,156,61,60,53,47,10],"ServerName":"****.com","SupportedCurves":[29,23,24],"SupportedPoints":"AA==","SignatureSchemes":[1025,1281,513,1027,1283,515,514,1537,1539],"SupportedProtos":null,"SupportedVersions":[771,770,769],"RemoteAddr":{"IP":"144.211.199.100","Port":50861,"Zone":""},"LocalAddr":{"IP":"144.211.199.100","Port":443,"Zone":""}}}}
2024/11/15 12:39:43.451 DEBUG   tls.handshake   choosing certificate    {"identifier": "****.com", "num_choices": 1}
2024/11/15 12:39:43.454 DEBUG   tls.handshake   default certificate selection results   {"identifier": "****.com", "subjects": ["****.com"], "managed": true, "issuer_key": "acme-v02.api.letsencrypt.org-directory", "hash": "ea07ac1ee1fbd60288d9138e6fe02ced1ded2b59c9b505a0203ea4ca85491ba2"}
2024/11/15 12:39:43.454 DEBUG   tls.handshake   matched certificate in cache    {"remote_ip": "144.211.199.100", "remote_port": "50861", "subjects": ["****.com"], "managed": true, "expiration": "2025/02/13 11:08:57.000", "hash": "ea07ac1ee1fbd60288d9138e6fe02ced1ded2b59c9b505a0203ea4ca85491ba2"}
2024/11/15 12:39:43.479 DEBUG   http.handlers.reverse_proxy     selected upstream       {"dial": "localhost:8001", "total_upstreams": 1}
2024/11/15 12:39:43.490 DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": "localhost:8001", "duration": 0.0082606, "request": {"remote_ip": "144.211.199.100", "remote_port": "50861", "client_ip": "144.211.199.100", "proto": "HTTP/1.1", "method": "GET", "host": "****.com", "uri": "/", "headers": {"X-Forwarded-Host": ["****.com"], "User-Agent": ["curl/8.10.1"], "Accept": ["*/*"], "X-Forwarded-For": ["144.211.199.100"], "X-Forwarded-Proto": ["https"]}, "tls": {"resumed": false, "version": 771, "cipher_suite": 49195, "proto": "", "server_name": "****.com"}}, "headers": {"Content-Type": ["text/html; charset=us-ascii"], "Server": ["Microsoft-HTTPAPI/2.0"], "Date": ["Fri, 15 Nov 2024 12:39:43 GMT"], "Content-Length": ["334"]}, "status": 400}

Error message:

Bad Request - Invalid Hostname
HTTP Error 400. The request hostname is invalid.

3. Caddy version:

v2.9.0-beta.3 h1:tlqfbJMRNY6vnWwaQrnWrgS+wkDXr9GIFUD/P+HY9vA=

4. How I installed and ran Caddy:

.\caddy.exe run

a. System environment:

Windows Server 2019

b. Command:

c. Service/unit/compose file:

d. My complete Caddy config:

{
debug
}
****.com, :9001 {
	reverse_proxy localhost:8001
}

# IP:9001 works, but ****.com returns bad request

5. Links to relevant resources:

I’ve disabled caddy on the actual server for now, because there was nothing more to see, if you need more info please just tell me

The response is coming from your upstream server because it doesn’t like that the Host header has the value kimiazi.com. Either fix the upstream server configuration/code to accept the host name kimiazi.com, or override it with the subdirective header_up Host <whatever upstream expects>.

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