Is there any way to make these multiple proxy into one line

I’m have webpack-dev-server setup with Caddy but the config is ugly since I need to repeat each path to passthrough transparent proxy. It seems that regex isn’t supported. Is there any way to combine these together without putting them all into their own directory prefix?

  proxy /webpack-dev-server localhost:8081 {
    transparent
  }
  proxy /webpack-dev-server.js localhost:8081 {
    transparent
  }
  proxy /js/ localhost:8081 {
    transparent
  }
  proxy /fonts/ localhost:8081 {
    transparent
  }
  proxy /css/ localhost:8081 {
    transparent
  }
  proxy /assets/ localhost:8081 {
    transparent
  }

  rewrite {
    if {path} not_match ^\/(assets|css|js|fonts|webpack-dev-server|webpack-dev-server.js)
    r ^/(.*)$
    to /index.php?_url={1}
  }

Sorry about the formatting :frowning:

Hi @dschissler, I’ve edited your post to add code blocks for readability.

You could try something like this:

rewrite {
  if {path} match ^/(assets/|css/|js/|fonts/|webpack-dev-server|webpack-dev-server.js)
  to /proxy{uri}
}
proxy /proxy localhost:8081 {
  transparent
  without /proxy
}

It’d probably be a fair bit faster, computationally speaking, to use multiple substring checks - and easier to read or modify, too:

rewrite {
  if_op or # rewrite should execute if ANY check is true
  if {path} starts_with /js/
  if {path} starts_with /css/
  if {path} starts_with /fonts/
  if {path} starts_with /assets/
  if {path} starts_with /webpack-dev-server
  to /proxy{uri}
}
proxy /proxy localhost:8081 {
  transparent
  without /proxy
}

The other rewrite would probably also benefit from substring checks instead - no need to use a regex capture group, since we can simply use the {path} placeholder:

rewrite {
  if_op and # rewrite shoule execute if ALL checks are true
  if {path} not_starts_with /js/
  if {path} not_starts_with /css/
  if {path} not_starts_with /fonts/
  if {path} not_starts_with /assets/
  if {path} not_starts_with /webpack-dev-server
  to /index.php?_url={path}
}

https://caddyserver.com/docs/proxy
https://caddyserver.com/docs/rewrite

Great. Thanks for giving three options.

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