How to reverse to https with http

1. Caddy version (caddy version):

caddy 2

2. How I run Caddy:

systemctl start caddy

a. System environment:

ubuntu 18.04

b. Command:

Paste command here.

c. Service/unit/compose file:

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane.

d. My complete Caddyfile or JSON config:

:45000 {
        handle /* {
                root * /home/mywork/CgsBim
                file_server

        }
        handle /api/* {
                reverse_proxy http://127.0.0.1:2020
        }

        handle_path /bimface/api/* {
                reverse_proxy https://api.bimface.com {
                        header_up X-Forwarded-Proto https
                }
        }


}

3. The problem I’m having:

When I POST http://39.99.156.137:45000/bimface/api/oauth2/token in my code, It returned an 302 to an http url. But the real api address is https://api.bimface.com , and I dont know why it redirect to an http address that seems not exists.

4. Error messages and/or full log output:

5. What I already tried:

So I add "header_up X-Forwarded-Proto https " , to reverse_proxy, but still got 302 result.
And I tried the https address with postman, This address https://api.bimface.com works fine.

6. Links to relevant resources:

You need to make sure that the Host header is correctly set. By default, Caddy passes through the Host header from the original request.

You can solve this by using a placeholder which takes the value of the currently selected upstream:

reverse_proxy https://api.bimface.com {
	header_up Host {http.reverse_proxy.upstream.hostport}
}

You can simplify this by removing the matcher altogether:

handle {

The difference is that with the matcher specified, Caddy will have to make a comparison against the request path on each request, which will always match (because the path always starts with /). Omitting it avoids that redundant check.

2 Likes

This Solved perfectly.

But handle / not work , It just result to a blank page. Not my index.html, and can’t find the real reason.
I will retry this later.

Thanks for your help.

1 Like

Don’t use /, just remove the matcher altogether. Use handle {.

Using / will only match requests to / exactly. Omitting it will match all requests.

1 Like

Yes, This config works well.
Thanks agian.

1 Like

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