Dynamically Route Traffic to a different port based off the path

1. The problem I’m having:

For the life of me, I cannot figure out how to route traffic to a specific port based off of the URL path. For example, if I have nullnet.services/abs/30000, what I want is for my Caddyfile to strip that 30000 and use it for the reverse_proxy localhost:30000 (programmatically, as these are dynamic)

2. Error messages and/or full log output:

No logs found.

3. Caddy version:

v2.6.4

4. How I installed and ran Caddy:

a. System environment:

Arch Linux LXC, x64, systemd

b. Command:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

c. Service/unit/compose file:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

d. My complete Caddy config:

# ==========================================
# youhaveno.dad domains here
# ==========================================

youhaveno.dad {
	tls /etc/ssl/certs/certificate4.pem /etc/ssl/private/key4.pem

    handle_path /abs/* {
        reverse_proxy 192.168.1.201:{http.request.uri.path.segment.0}
    }

    handle {
        reverse_proxy 192.168.1.201:9001
    }
}

api.youhaveno.dad {
	# @blocked not remote_ip 68.102.54.197
	# respond @blocked "<h1>Access Denied</h1>" 403
	tls /etc/ssl/certs/certificate4.pem /etc/ssl/private/key4.pem
	reverse_proxy 192.168.1.201:9000
}

Figured it out!

Instead of {http.request.uri.path.segment.0}, it should have been {http.request.uri.path.0}.

1 Like

That seems kinds dangerous. Make sure you have absolutely no other servers on that machine that a user could exploit.

1 Like

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