Forward Requests to a sub-domain via CaddyFile

1. Caddy version:

v2.6.2

a. Systebunm environment:

OS: Linux
Distro: Ubuntu
Running Caddy via Docker Compose

b. Command:

sudo service <Caddy-Server-Name> restart

c. Service/unit/compose file:

version: "3.7"
services:
  caddy:
    image: caddy:2
    restart: unless-stopped
    network_mode: "host"
    volumes:
      - <volume-path>
      - caddy_data:/data
      - caddy_config:/config
volumes:
  caddy_data:
  caddy_config:

d. My complete Caddy config:

blog.demo.com {
       redir https://www.demo.com/blog{uri}
}
demo.com {
         redir https://www.demo.com{uri}
}
www.demo.com {
        redir /blog /blog/

}

3. The problem I’m having:

I have a domain. Let’s say demo.com. I have to forward requests of a route of this domain to a subdomain and render response of the same without Changing URL via Caddy Server
For Example,

Request Flow:
demo.com/homeCaddyhttps://test.demo.com/home

Response Flow:
demo.com/homeCaddyhttps://test.demo.com/home

Paste logs/commands/output here.
USE THE PREVIEW PANE TO MAKE SURE IT LOOKS NICELY FORMATTED.



### 5. What I already tried:
I tried following `reverse_proxy` rules to achieve this but was unable to achieve the desired response.

```JSON
www.demo.com {
     reverse_proxy /home https://test.demo.com/home {
                #proxy to subdomain
                header_up Host test.demo.com
     }
}

This crashes the whole site. I think the reason for this is that Caddy doesn’t support such type of Upstream Addresses. Valid addresses can be seen here.

Then, I tried the valid one -

www.demo.com {
     reverse_proxy /home https://test.demo.com {
                #proxy to subdomain
                header_up Host test.demo.com
     }
}

This didn’t led to any error but this didn’t bring me any response either. The reason for this can be that I am forwarding the request from Client to another Client and not the actual server.

Hence, I tried to hit the IP address associated with the sub domain

www.demo.com {
     reverse_proxy /home <IP-address> {
                #proxy to subdomain
                header_up Host test.demo.com
     }
}

This changes the actual route i.e. in the web-browser it changes the URL from demo.com/home to test.demo.com. But it doesn’t brought any response either.

Kindly comment if there is any possible solution for forwarding the request to a sub-domain route.
Kindly do tell if this case is not supported by CaddyFile.

Try this:

www.demo.com {
	reverse_proxy https://test.demo.com {
		header_up Host {upstream_hostport}
	}
}

The reverse_proxy directive doesn’t change the URL at all by default.

I think your problem was that you were using a path matcher /home, which only matches /home exactly, and nothing else.

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