How to route a separate app through the same domain using different path

1. Caddy version (caddy version):

v2.4.5

2. How I run Caddy:

I use the default Systemd caddy.service unit.

a. System environment:

OS: Ubuntu 20.04.3 LTS x86_64
Kernel: 5.4.0-89-generic
SystemD

b. Command:

No extra commands to manage caddy are used other than restarting the systemd unit.

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

i.toxicaven.dev {
        reverse_proxy localhost:40115
}
toxicaven.dev {
        reverse_proxy localhost:3000
}
haste.toxicaven.dev {
        reverse_proxy localhost:7777
}

3. The problem I’m having:

I am attempting to have an express.js app (currently being served to i.toxicaven.dev via port 40115) run through toxicaven.dev/images without modifying what is being served to toxicaven.dev by a different express app.

4. Error messages and/or full log output:

No errors are provided, using any of the tried methods, aside from Internal Server Error when attempting to access the toxicaven.dev/images endpoint.

5. What I already tried:

toxicaven.dev {
        route /images* {
              reverse_proxy localhost:40115
       }
        reverse_proxy localhost:3000
}
toxicaven.dev/images {
        reverse_proxy localhost:40115
}

6. Links to relevant resources:

1 Like

In case you didn’t see it, please see this page in the docs:

That’s essentially the way to do it, but I’d adjust it to this:

toxicaven.dev {
	handle /images* {
		reverse_proxy localhost:40115
	}

	handle {
		reverse_proxy localhost:3000
	}
}

Turn on the debug global option in Caddy, the logs should show additional details, if it’s a problem with proxying.

Add this at the top of your Caddyfile:

{
	debug
}

“Internal Server Error” probably means there was a problem with your express upstream app, though.

1 Like

Thank you for the response! Unfortunately, this does not appear to work correctly, for some odd reason. using the caddyfile

i.toxicaven.dev {
        reverse_proxy localhost:40115
}
toxicaven.dev {
        handle /haste* {
                reverse_proxy localhost:7777
        }

        handle {
                reverse_proxy localhost:3000
        }
}
haste.toxicaven.dev {
        reverse_proxy localhost:7777
}

allows the Hastebin to load when using haste.toxicaven.dev, but causes serious serving issues when using toxicaven.dev/haste, although it does load the page. No abnormalities can be found with the normal toxicaven.dev page. I will leave the caddyfile configuration enabled for now, as I am unsure of what could be causing this. No errors or anything worth noting in the journalctl for caddy or the hastebin server.

When you use handle, the request path will be preserved, i.e. /haste will still be prefixed on the path sent upstream. Is that what you’re expecting to happen? Or are you expecting /haste to be stripped off first?

If you need to strip the path prefix, you can use handle_path instead of handle.

Some additional reading, somewhat tangential:

1 Like

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