Caddy reverse proxy for multiple ports

1. Caddy version (caddy version):

v2.4.3

2. How I run Caddy:

installed caddy from here -

curl -o caddy  "https://caddyserver.com/api/download?os=linux&arch=amd64&idempotency=3754473143692"

a. System environment:

I’m running it inside a docker container

b. Command:

caddy run

d. My complete Caddyfile or JSON config:

:8000 {
    reverse_proxy 127.0.0.1:3000
    reverse_proxy 127.0.0.1:3004
}  

3. The problem I’m having:

I’m trying to use expose multiple ports using reverse proxy. these endpoints {internnal_IP:Ports} are used in a web application. I’m facing frequent 404s while executing all the steps. I can say it loads 2/5 times.
Also I see a Gateway Timeout error in the last stage of the application.
The application works fine while running in localhost. But I’m not able to figure out how to run this inside the docker container

4. Error messages and/or full log output:

There are no error messages in the Caddy log or even in the application logs. just a Gateway timeout or 404 in the web page

5. What I already tried:

trial and error with no luck. didn’t find anything similar to my issue anywhere. using Caddy for the first time I’m not sure if there is an issue with the Caddyfile or the container config

6. Links to relevant resources:

If you’re using it Docker, please consider using the official docker image, instead of straining the download server, which has limited resources. See Docker

You haven’t told Caddy when to use either proxy. It’s unclear what you’re trying to do here.

Do you want load balancing between the two backends, or do you want some requests to hit one and other requests to hit the other?

If you want to load balance, then list out the upstreams like this:

reverse_proxy 127.0.0.1:3000 127.0.0.1:3004

If you need to make routing decisions, use request matchers:

Also if you’re running this in a Docker container, be aware that 127.0.0.1 will only make requests to the same container. So if you’re trying to proxy to another container, this won’t work. Instead, use the container name of the other container as the upstream address (which Docker’s internal DNS server will resolve to the IP of the other container).

I want some requests to hit one and other requests to hit the other.
I have tried using the load balancing config this way

:8000{
reverse_proxy 127.0.0.1:3000 127.0.0.1:3004
}

But still facing the same issue.

  • I’m not trying to proxy to another container. It’s all happening within the same container

  • How do we clarify Caddy when to use either proxy? For example how right now I can access my end point as {APP_URL}/endpoint. How can I configure Caddyfile to make it reach {APP_URL}:3000/endpoint and {APP_URL}:3004/endpoint ?

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