Passing variable query parameters to application running on a specific port

What already works

Visiting / should show a normal website (works)

pllx.eu:80 {
        tls off
        root pllx.eu-website/
}

Visiting /statistics should show a proxied website (works)

pllx.eu:80/statistics {
        proxy / http://localhost:3000 {
                header_downstream Content-Type text/plain
        }
}

What doesnt work / What I tried / How it should work

Visiting pllx.eu/id/key should send a file
The problem is that id and key are variables and not static values

Visiting https://pllx.eu/EWkvBH/JzTUXuWQJqDGdzj3 should show an image or send a file

Its a regular GET request on /:id/:key

pllx.eu:80/{id}/{key} {
        tls off
        proxy / http://localhost:3000 {
                header_upstream Host {host}
                header_upstream X-Real-IP {remote}
                header_upstream X-Forwarded-For {host}
                header_upstream X-Forwarded-Proto {scheme}
                header_downstream Content-Type {>Header}
        }
}

Sending a POST request on /upload should give you a text/plain callback, but I think I can figure this out by myself when someone of you can help me with the variable query problem.

Thanks to everyone who answers, I love caddyserver.

~ Stan

Hmm. Checking the Caddyfile spec, I don’t think that you can structure a label like that (with the {id} and {key}).

You could use internal rewriting and a regex check for your conditional proxy. Something like:

pllx.eu:80 {
  ## Not required as the port is explicitly :80, disabling HTTPS
  # tls off
  root pllx.eu-website/

  rewrite {
    r ^\/\w+\/\w+\/?$ # or other regex as appropriate, this one matches `/EWkvBH/JzTUXuWQJqDGdzj3`
    to /proxy{path} # simply appends `/proxy` so it'll get picked up by the proxy
  }

  proxy /proxy localhost:3000 {
    transparent # covers all the header_upstream subdirectives
    header_downstream Content-Type {>Header}
    without /proxy # don't send the /proxy part upstream
  }
}
2 Likes

It works, thank you very much. :slight_smile:

2 Likes

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