How to reverse_proxy to server with prefix path

1. Caddy version (caddy version):

2.2

2. How I run Caddy:

systemctl start caddy

a. System environment:

ubuntu 1804

b. Command:

paste command here

c. Service/unit/compose file:

paste full file contents here

d. My complete Caddyfile or JSON config:

80: {
   handle /api/* {

   rervers_proxy  http://127.0.0.1/prefix
   }

}




3. The problem I’m having:

I want the requested url: http://MYHOST/api/get to proxy to http://127.0.0.1/prefix/api/get
But I can’t do this with “rervers_proxy http://127.0.0.1/prefix” in my Caddyfile

4. Error messages and/or full log output:

Caddfile Parsing error

5. What I already tried:

6. Links to relevant resources:

You’ll want to use a rewrite to prepend to the path before proxying:

:80 {
	handle /api/* {
		rewrite * /prefix{uri}
		reverse_proxy http://127.0.0.1
	}
}

But this Caddyfile doesn’t actually make sense because http://127.0.0.1 will essentially be a loopback from Caddy back onto itself, because it’s listening on :80. so you’ll need to fix that.

It works, Thank you for your help!

1 Like

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