Reverse proxy question

Hi guys. I have a little problem with the reverse proxy. In my caddyfile, i have a simple reverse proxy setup:

reverse_proxy /rp https://www.google.com

The problem with this config is that the proxy works but it redirects to https://www.google.com/rp

Is there any way to remove the /rp from the target address?

Than you

Yup! There’s a strip_prefix directive meant for exactly this use-case!

strip_prefix /rp/* /rp

Thank you for the answer. If i insert your code in the caddy file, the proxy doesn’t redirect anymore.
Caddyfile example:

localhost:2020 {
file_server browse
reverse_proxy /rp  https://google.it
strip_prefix /rp/* /rp
}

Ah, I think you’ll need to do it like this:

localhost:2020 {
    file_server browse
    route /rp/* {
        strip_prefix rp/
        reverse_proxy https://google.it
    }
}

See the route docs and the example: https://caddyserver.com/docs/caddyfile/directives/route#examples

Doesn’t work either… I think that the /rp must be deleted after reverse proxy and not before.

It’ll be too late at that point.

By the time the reverse_proxy line is done, the request processing is terminated as the response from upstream is returned. If you manually order anything after a terminating handler, it will never execute.

That said, the latest example won’t work for a request to /rp because the route is configured for /rp/* (note the trailing slash, which it will be picky about). That may be why it’s not working for you.

Try:

route /rp* {
  strip_prefix /rp
  reverse_proxy https://google.it
}

Note that this version will also work on /rpfoo as well as /rp and /rp/foo etc.

1 Like

Still nothing, doesn’t work. And this line must be
strip_prefix rp/

becouse like this

strip_prefix /rp

i have this error on run:

using adjacent Caddyfile
run: adapting config using caddyfile: parsing caddyfile tokens for 'route': Caddyfile:4 - Error during parsing: parsing caddyfile tokens for 'strip_prefix': Caddyfile:4 - Error during parsing: Wrong argument count or unexpected line ending after 'strip_prefix'

Try strip_prefix rp; using rp/ will cause this directive to do nothing for a request to literally /rpfoo or /rp.

If a matched request does not have the given path prefix, this directive is a no-op.
—https://caddyserver.com/docs/caddyfile/directives/strip_prefix

I’m not sure why the Caddyfile parser is giving you that error. It’s a valid argument count. It might be a bug.

With this the redirect works but it’s still wrong: it always redirect to www.google.it/rp

I can’t replicate this behaviour, although I did identify and solve another problem - Google would 404 me every time because the Host header sent upstream, by default, is the original client-requested localhost:2020. I had to override it to specify I wanted the upstream host.

My Caddyfile:

localhost:2020 {
  file_server browse
  route /rp* {
    strip_prefix rp
    reverse_proxy https://google.it {
      header_up Host {http.reverse_proxy.upstream.hostport}
    }
  }
}

The request I made:

~/Projects/test
âžś curl -IL localhost:2020/rp
HTTP/1.1 301 Moved Permanently
Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
Cache-Control: public, max-age=2592000
Content-Length: 219
Content-Type: text/html; charset=UTF-8
Date: Fri, 13 Mar 2020 00:16:38 GMT
Expires: Sun, 12 Apr 2020 00:16:38 GMT
Location: https://www.google.it/
Server: Caddy
Server: gws
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 0

HTTP/2 200
date: Fri, 13 Mar 2020 00:16:39 GMT
expires: -1
cache-control: private, max-age=0
content-type: text/html; charset=ISO-8859-1
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
set-cookie: 1P_JAR=2020-03-13-00; expires=Sun, 12-Apr-2020 00:16:39 GMT; path=/; domain=.google.it; Secure
set-cookie: NID=200=BIPfZZTLi-lHpulux9_tVVK45z55etbLJQWhuKzMLM7CIwNhhzRgYmcxhDrAZJdjWGsAUV007y7UrqBAWEVb9YbZfQvuioHcA9yUcEtK8L5DHVBR0QJYlbbLk4ajZl6F3NhCBnGXX3CrpjZdHOSoX_rZnED-TjsjCJDNnlEoK9o; expires=Sat, 12-Sep-2020 00:16:39 GMT; path=/; domain=.google.it; HttpOnly
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
accept-ranges: none
vary: Accept-Encoding

As you can see, I have a Location redirect straight to the main domain.

I was able to replicate the bug with strip_prefix not liking a single argument if it starts with a leading slash.

1 Like

Finally, with this last solution, redirect works as it should! Thank you very much! Another little question: is it possible to redirect the internal fileserver to something like localhost/fs or localhost/fileserve or whatever?

Yes. file_server accepts a matcher.

Could you provide an example? thank you

There are examples in the linked documentation, including how to use one with a matcher: https://caddyserver.com/docs/caddyfile/directives/file_server#examples

1 Like

Sorry, something wrong.
If I try multiple reverse proxy, just like that

:2020 {
    root * ./dataset
    file_server
    route /rp* {
        strip_prefix rp
        reverse_proxy http://localhost:2021
    }
    route /rpa* {
        strip_prefix rpa
        reverse_proxy http://localhost:2023
    }
    route /rpc* {
        strip_prefix rpc
        reverse_proxy http://localhost:2022
    }    
}

:2021 {
    root * ./hahah
    file_server
}

:2022 {
    root * ./docker-typecho
    file_server browse
}

:2023 {
    root * ./examples
    file_server browse
}

Yes, it can get the page, but you can’t get the subpath,

Like you can get the localhost:2020/rpa, and you want get the localhost:2020/rpa/example.

But it jumps to localhost:2020/example

And then I try the config use

...
route /rpa/* {
...
}
...

It will get the subpath, but cant’t use localhost:2020/rpa to access. Must use localhost:2020/rpa/

This Caddfile doesn’t seem to be universal.


And when I try add root /xx ./others in the :2020 config, it will be wrong, return the code 400.

Can I get your help?

Looks like a case of the subfolder problem I’ve described in the past:

The other one sounds like it’s a bit more involved.

I suggest you create a new Help thread and fill out the template.

Excuse me again. I’m trying to reproduce the same configuration with caddy v1. I’ve tried with the option “without” but it doesn’t work. Could you post the same config for v1? Thank you again.

Can you post the v1 Caddyfile you tried?

When you say “doesn’t work” - can you elaborate? What did you do, what result did you expect, and what result did you get instead? Be specific (quote any requests you made and exact errors returned).

@Whitestrake
Yep sorry, you are right: i was too much synthetic. Doesn’t work means that i can’t get rid of the /path during a reverse proxy configuration. Caddyfile V1 configuration is:

DOMAIN:PORT {
proxy / http://WEBSITE-A {
transparent
websocket
}
 proxy /rp http://WEBSITE-B {
 transparent
 websocket
 }
}

With this configuration when i browse to:

http://DOMAIN:PORT/rp

the proxy redirects to

http://WEBSITE-B/rp (wrong address). While, if i add the without section like this:

proxy /rp http://WEBSITE-B {
without /rp     
transparent
websocket
}

when i browse to

http://DOMAIN:PORT/rp

the proxy redirects it to

http://WEBSITE-A

Same as before i’m try to achieve a reverse proxy like this:

http://DOMAIN:PORT/rp ===> http://WEBSITE-B (without /rp)

Long shot here, but if it’s related to the previously discussed issue…

Try removing transparent?

e.g.

example.com {
  proxy /rp http://website-b.example.com {
    without /rp
    websocket
  }
}

P.S. To clarify on this point:

Caddy’s proxy does NOT issue a redirect. If you’re getting redirected (such as if you proxy to Google), it’s coming from upstream. The distinction is very important when you’re trying to achieve a specific result.