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.
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.
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: file_server (Caddyfile directive) — Caddy Documentation
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
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.
I already tried without transparent and without websocket, still the same result. Btw, the target of the reverse proxy is not something like website-b.example.com: in the browser the address will always stays at example.com.
Yep, right: the address will not change to the WEBSITE-B’s address. I used the wrong terminology.
I tried to replicate the problem with the simplest possible configuration, but it worked exactly as expected.
~/Projects/test
➜ mkdir website1 website2
~/Projects/test
➜ echo "I'm website 1!" > website1/index.txt
~/Projects/test
➜ echo "I'm website 2!" > website2/index.txt
~/Projects/test
➜ cat Caddyfile
:8080 {
proxy / localhost:8081
proxy /rb localhost:8082 {
without /rb
}
}
:8081 {
root website1
}
:8082 {
root website2
}
~/Projects/test
➜ caddy -version
v1.0.4
~/Projects/test
➜ curl localhost:8080
I'm website 1!
~/Projects/test
➜ curl localhost:8080/rb
I'm website 2!
This is very strange indeed. I replicate your simple example and it works. But when i try with this Caddyfile, it doesn’t…
example.com:443 {
header / Strict-Transport-Security "max-age=31536000"
proxy / https://INTERNAL-IP-A {
transparent
websocket
insecure_skip_verify
}
proxy /rp INTERNAL-IP-B {
without /rp
}
}
Maybe external webservers manage those type of requests in a different way…
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.