How do I reverse-proxy to diffirent docker containers

1. Output of caddy version:

I use docker image caddy:2

2. How I run Caddy:

a. System environment:

Ubuntu server 22.04
Docker 20.10.12

b. Command:

docker run --rm --netowrk host caddy

c. Service/unit/compose file:

Dockerfile

FROM caddy:2

WORKDIR /app

COPY Caddyfile .

CMD ["caddy", "run"]

d. My complete Caddy config:

allure.myproject.dev {
    handle_path /14 {
        rewrite /14 /latest-report
        reverse_proxy :5058
    }
    handle_path /13 {
        rewrite /13 /latest-report
        reverse_proxy :5056
    }
}

3. The problem I’m having:

I have 10 docker containers with allure-report. For example i use only two.
I need to proxy requests to specific ports, depending on the transmitted URI
For example:
GET allure.myproject.dev/14 → :5058/latest-report
GET allure.myproject.dev/13 → :5056/latest-report

But config in my Caddyfile dont work as i want. When i make request, i see blank page in response and status code 200. But when i change config to below, i see what i need (I make request on allure.myproject.dev/latest-report)

allure.myproject.dev {
        reverse_proxy :5058
}

what should i do in my case? Cant find information.

4. Error messages and/or full log output:

5. What I already tried:

I tried next configurations:

(dont work)

allure.myproject.dev {
    handle_path /14 {
        reverse_proxy :5058/latest-report
 }

(dont work, i see blank page)

allure.myproject.dev {
   handle_path /14 {
        rewrite /14 /latest-report
        reverse_proxy :5058
    }
 }

6. Links to relevant resources:

Remove this from your Dockerfile. The default command is ideal.

You should copy this to /etc/caddy/Caddyfile instead, which is what the default command uses.

Path matching is exact in Caddy. This means that only the path /14 will match, and nothing else. If you mean to proxy everything under that subpath, then use /14* instead.

Since handle_path strips the path prefix before continuing (the handle directive doesn’t strip the path), the path no longer is /14 at this point. So instead, you should do rewrite * /latest-report if you want to rewrite every request that reaches this handle.

1 Like

Thanks for help.
I tried edit my Caddyfile to

allure.myproject.dev {
     handle_path /14 {
          rewrite * /latest-report
          reverse_proxy 127.0.0.1:5058
     }
}

And it works, but when i added second docker-container in Caddyfile, I see blank page again.

And it works, but when i added second docker-container in Caddyfile, I see blank page again.

What does the Caddyfile look like after this edit?

What URL are you trying to access after this edit that gives you a blank page?

Caddyfile

allure.myproject.dev {
    handle_path /14 {
        rewrite * /latest-report
        reverse_proxy :5058
    }
    handle_path /13 {
        rewrite * /latest-report
        reverse_proxy :5056
    }
}

I trying https://allure.myproject.dev/13, https://allure.myproject.dev/14

UPD.
i need to reverse-proxy requests from allure.myproject.dev/13 to 127.0.0.1:5056/latest-report and allure.myproject.dev/14 to 127.0.0.1:5058/latest-report

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