umputun / reproxy

Simple edge server / reverse proxy
http://reproxy.io
MIT License
1.23k stars 92 forks source link

[Feature request]get host in route rule #189

Closed leopku closed 4 months ago

leopku commented 4 months ago

It seems reproxy doesn't support get host in route rule.

For example:

- { route: "^/api/svc1/(.*)", dest: "http://s3.my.domain/public/${host}/blah1/$1" }
umputun commented 4 months ago

Not sure I understand the request. I guess this is about the static file provider, and if this is the case, you can already define the host, not as part of the rule itself you provided, but rather as a higher-level "key".

default:
  - {route: "^/api/svc1/(.*)", dest: "http://127.0.0.1:8080/blah1/$1"}
  - {route: "/api/svc3/xyz", dest: "http://127.0.0.3:8080/blah3/xyz", "ping": "http://127.0.0.3:8080/ping"}
  - {route: "/web/", dest: "/var/web", "assets": yes, "keep-host": yes,  "remote": "192.168.1.0/24, 124.0.0.1"}
  - {route: "/web2/", dest: "/var/web2", "spa": yes, "keep-host": no}
srv.example.com:
  - {route: "^/api/svc2/(.*)", dest: "http://127.0.0.2:8080/blah2/$1/abc"}

This config defines two sets of rules - one for srv.example.com and another (default) as a catch-all-others

leopku commented 4 months ago

Not sure I understand the request. I guess this is about the static file provider.

Yes, in my case that is for serving static files. But as mentioned in the example, the dest was an s3 server for multi-domains.

you can already define the host

in the case of wildcard sub-domain, i don't know the host exactly.

more clearly explain:

there are many sub-folders named by domain name for multi-sites in some bucket of s3. Request should be proxied to different sub-folders.

*.cdn.example.com
  - { route: "^/statics/(.*)", dest: "http://s3.my.domain/public/${host}/blah1/$1" }
leopku commented 4 months ago

It very useful for both static and dynamic upstreams in wildcard domains case.

umputun commented 4 months ago

Thank you for the clarification, it makes sense. I initially missed the key part with $host from my first look.

umputun commented 4 months ago

The change is on the master branch. It supports both $host and ${host} parameters. This functionality was implemented at the low level (discovery.Match) and should work for all providers, including docker, static, and file.

Give it a try and let me know if it works as described.

leopku commented 4 months ago

got "Server error"

config

"*.s3.nas.home":
  - {route: "^/(.*)", dest: "http://s3.nas.home:9000/www/$host/$1"}

logs:

2024/05/22 13:17:02.420 [WARN]  {discovery/discovery.go:238 discovery.findMatchingMappers} invalid regexp *.s3.nas.home: error parsing regexp: missing argument to repetition operator: `*`
2024/05/22 13:17:02.420 [WARN]  {discovery/discovery.go:238 discovery.findMatchingMappers} invalid regexp *.s3.nas.home: error parsing regexp: missing argument to repetition operator: `*`
2024/05/22 13:17:02.420 [WARN]  {proxy/proxy.go:255 proxy.(*Http).proxyHandler.func2} no match for  /index.html
2024/05/22 13:17:02.451 [WARN]  {discovery/discovery.go:238 discovery.findMatchingMappers} invalid regexp *.s3.nas.home: error parsing regexp: missing argument to repetition operator: `*`
2024/05/22 13:17:02.451 [WARN]  {discovery/discovery.go:238 discovery.findMatchingMappers} invalid regexp *.s3.nas.home: error parsing regexp: missing argument to repetition operator: `*`
2024/05/22 13:17:02.451 [WARN]  {proxy/proxy.go:255 proxy.(*Http).proxyHandler.func2} no match for  /favicon.ico
umputun commented 4 months ago

I doubt it even worked with a wildcard in the server (hostname), and it is likely unrelated to the changes. Probably making it a regular expression will do the trick, i.e., .*.s3.nas.home.

As I have some time, I will add patterns too, but for now they are not supported.

leopku commented 4 months ago

as ur words, it works as changing host to .*.s3.nas.home. i will keep this pattern until wildcard pattern supported.

thanks for all ur works.