I am trying to run a server where static files are served from the root and API calls are made through /api/<path> using the following Caddyfile:
my.domain.com {
route /api/* {
uri strip_prefix api/
reverse_proxy http://localhost:8074
}
route {
root * /app
try_files {path} /index.html
file_server
}
}
That was the best I could from the docs, #3064 points that they lack examples and more details already.
This configuration leads to a server previously running fine under Nginx to respond every request with 400 Bad Request.
My caddy daemon runs inside a Docker container, my first step was to check if the server proxied was actually being hit, and adding a couple logs I figured out that no, the request never reaches the server, so caddy must be interfering.
I checked that the proxied server is really reachable from within the container, using curl http://localhost:4321 correctly reaches the proxied server, and it responds as it should.
Even the simplest of servers can be used to reproduce it:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
})
http.ListenAndServe(":8074", nil)
}
Any variation of localhost, be it 127.0.0.1 or 0.0.0.0, does not work either.
I think this might be because your strip_prefix is incorrect. Try the following instead:
uri strip_prefix /api
How unfortunate. The documentation itself uses the incorrect syntax, and I can confirm that was the culprit.
You're right. That's outdated. I'll get that fixed, thanks for pointing it out!
The uri docs does have the correct syntax though: https://caddyserver.com/docs/caddyfile/directives/uri#examples
Edit: Fixed here https://github.com/caddyserver/website/commit/430602e0638fb04a7f3a96c70347cbd328f36586
Most helpful comment
You're right. That's outdated. I'll get that fixed, thanks for pointing it out!
The
uridocs does have the correct syntax though: https://caddyserver.com/docs/caddyfile/directives/uri#examplesEdit: Fixed here https://github.com/caddyserver/website/commit/430602e0638fb04a7f3a96c70347cbd328f36586