caddyserver / caddy

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
https://caddyserver.com
Apache License 2.0
57.93k stars 4.02k forks source link

Placeholders do not work as upstream address for reverse_proxy #6254

Closed roger6106 closed 6 months ago

roger6106 commented 6 months ago

When using a placeholder as an address for reverse_proxy, it appears to prefix the domain name with a /.

This can be replicated with this simple caddyfile:

http://:1111

vars location https://www.yahoo.com
reverse_proxy {vars.location}

The console error shows:

ERROR http.log.error dial tcp: lookup /www.yahoo.com: no such host {"request": {"remote_ip": "::1", "remote_port": "51245", "client_ip": "::1", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:1111", "uri": "/", "headers": {"User-Agent": ["curl/8.4.0"], "Accept": ["/"]}}, "duration": 0.001612083, "status": 502, "err_id": "gndfwzj08", "err_trace": "reverseproxy.statusError (reverseproxy.go:1267)"}

As for why I can't use an alternative approach, the goal is to do some dev work that needs to reverse_proxy paths from various servers. Here's a simplified version of what I am trying to accomplish (using public sites for simplicity):

http://:1111

reverse_proxy http://www.yahoo.com {
    header_up Host www.yahoo.com

    @redirect status 3xx
    handle_response @redirect {
        @location vars_regexp location {rp.header.Location} (https?://[a-zA-Z0-9-.]+)(.*)
        reverse_proxy @location {re.location.1}
    }
}

Caddy --version is v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A

francislavoie commented 6 months ago

Upstream address must not contain a scheme when using placeholders, it needs to be only the host & port. See https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#upstream-addresses

If the address is not a URL (i.e. does not have a scheme), then placeholders can be used, but this makes the upstream dynamically static, meaning that potentially many different backends act as a single, static upstream in terms of health checks and load balancing. We recommend using a dynamic upstreams module instead, if possible. When using placeholders, a port must be included (either by the placeholder replacement, or as a static suffix to the address).

If you want to configure HTTPS, then add this to your proxy config:

transport http {
    tls
}

And you may also need header_up Host {upstream_hostport} as per https://caddyserver.com/docs/caddyfile/directives/reverse_proxy#https

For next time, please ask your usage questions on the Caddy community forums. We prefer to keep the GitHub issue board for bugs and feature requests. Don't forget to fill out the thread template so we can help you!

roger6106 commented 6 months ago

Thank you! I was sure there had to be a way. The error showing a lookup of lookup /www.yahoo.com was what was throwing me off, especially since I was able to use a schema when I inserted it directly.