How to set up a reverse proxy for a non-local host, I didn't find any relevant examples in the official documentation

1. The problem I’m having: I want to set up a reverse proxy in Caddy that points to a non-local host, how should I do it?

for example:
http://iot.a.com {

reverse_proxy 117.x.99.x:9080

}

2. Error messages and/or full log output:

Failed to load resource: the server responded with a status of 503 ()

3. Caddy version:

v2.7.4 

4. How I installed and ran Caddy:

docker

a. System environment:

debian11

b. Command:


d. My complete Caddy config:

http://iot.youes.com {
	# Set this path to your site's directory.
	#root * /usr/share/caddy



	# Another common task is to set up a reverse proxy:
	reverse_proxy 117.131.99.2:9080
	encode gzip

	# Or serve a PHP site through php-fpm:
	# php_fastcgi localhost:9000
}

5. Links to relevant resources:

You’ll want the http:// prefix in the reverse_proxy directive instead of at the top of the address block.

i.e.

iot.youes.com {
    reverse_proxy http://117.131.99.2:9080
}

It gets a bit more complicated if you have a wildcard cert and are using TLS certificates:

*.youes.com, youes.com {
    tls {
        dns porkbun {
            api_key {env.PORKBUN_API_KEY}
            api_secret_key {env.PORKBUN_API_SECRET}
     }

    @iot host iot.youes.com {
    handle @iot {
        reverse_proxy http://171.131.99.2:9080
    }
}

Note that you may have to recompile Caddy with the appropriate plugin for your provider…

Thank you for your guidance and reply. iot.youes.com is a subdomain, can I turn off the security certificate only on this subdomain?Because other sites on youes.com are also in use, some of them are using HTTPS.

For the record, this isn’t accurate

The reverse_proxy directive assumes http. Only the site address (See Caddyfile Concepts — Caddy Documentation) assumes HTTPS.

But why? Caddy will manage the certificates automatically. There isn’t any extra maintenance effort.

They don’t conflict with each other though, unless there’s more information that isn’t in this thread.

2 Likes

Thank you for your guidance and reply. How should I configure the subdomain iot.youes.com, whether to use HTTP or HTTPS?

You shouldn’t overthink it. Configure it without protocols as such:

example.com {
    # site configuration
}

Caddy will redirect all HTTP requests to HTTPS then serve the requested content.

I feel like you need to read through the docs to better know how Caddy behaves and how to configure it.

2 Likes

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