Routing rules for host header

1. Caddy version (caddy version):

2.3.0 / 2.4.0-beta.2

2. How I run Caddy:

a. System environment:

Operating System: KDE neon 5.21
Kernel Version: 5.4.0-70-generic
OS Type: 64-bit
Docker version 20.10.6, build 370c289
curl 7.68.0

b. Command:

Tried both of them

./caddy --config Caddyfile

docker run --rm --name test-caddy --mount type=bind,source="$(pwd)/Caddyfile",target=/srv/Caddyfile --network host caddy:2.4.0-beta.2-alpine caddy run

c. Service/unit/compose file:

d. My complete Caddyfile or JSON config:

http://localhost:3010 {

        @uaprom {
                path /graphql
        }
        reverse_proxy @uaprom https://prom.ua {
                transport http {
                        tls_insecure_skip_verify  # not affect
                }
        }
}

3. The problem I’m having:

Trying to proxy requests from my local app to external site for development purposes. But we need Host header to be set and it can differ from requested URL. Please can you sugges any help with if it is possible. Currently I didn’t find any mentions of this problem in other topics. Thank you

4. Error messages and/or full log output:

Expected behavior:

curl -v -H "Host: prom"  localhost:3010/graphql

should return 405 status code, as request really proxied to prom.ua/graphql

Current behavior:
Caddy return default 200 status code, as request not matched to Caddyfile rules

$ curl -v -H "host: prom.ua" localhost:3010/graphql
...
* Connected to localhost (::1) port 3010 (#0)
> GET /graphql 
> Host: prom.ua
> User-Agent: curl/7.68.0
> Accept: */*
>

< HTP/1.1 200 OK
< Server: Caddy
< Date: Mon, 12 Apr 2021 18:33:39 GMT
< Content-Length: 0

Notes:
Removing Host header allow to get correct response (404 status code), so I can’t get why there are troubles with matching Host header requests

$ curl -v localhost:3010/graphql                    
...
> GET /graphql 
> Host: localhost:3010
> User-Agent: curl/7.68.0
...
< HTP/1.1 405 Method Not Allowed
< Allow: POST
< Server: Caddy
< Server: nginx
< Set-Cookie: ....

<html>
...
<h1>405 Method Not Allowed</h1>
...

5. What I already tried:

header_up Host prom.ua

matcher rules on header Host *

Tried to wrap it as handler handle @uaprom {

6. Links to relevant resources:

I think you’re looking for this example in the docs:

reverse_proxy localhost:9000 {
	header_up Host {http.reverse_proxy.upstream.hostport}
}

If you’re proxying over HTTPS to another domain, you need to make sure the Host header is properly set, and in this case it sets it to the configured upstream hostname, i.e. prom.ua. Like this:

http://localhost:3010 {
	reverse_proxy /graphql https://prom.ua {
		header_up Host {http.reverse_proxy.upstream.hostport}
	}
}

If you make a curl request while overriding the Host header, then Caddy won’t match localhost (your site address).

We override Host header at client side on purpose, so we can proxy local http request to public upstream backend on the internet with https. Yes, that is the problem, Caddy not matching overriden Host header
Okay, thank you for response

So yeah, just let Caddy do that for you instead. Stop having your client change the Host header, and configure Caddy to do it instead for the proxy request.

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