Reverse Proxy 404 Issue

To understand why this is happening, you have to understand a few points:

  • When a web server tries to find a file to serve, it takes the web root and the URI together to determine which file you want.
  • When you specify a path for a proxy, the path you give is simply a prerequisite for Caddy to proxy the entire request to that backend.
  • When Caddy proxies to a backend, it naturally forwards the entire requested URI along with it.

So lets work through what happens when you send a request for /project-one to Caddy on port 80. Caddy:

  • matches the request to a site block (http://)
  • checks the URI (/project-one) and sees that this request qualifies to be proxied
  • makes a new request to localhost:7000/project-one
  • receives its own request on port 7000 and uses the web root + URI to find a file to serve
  • looks in /Users/me/Desktop/www/project-one/project-one and presumably finds nothing
  • responds to itself on port 7000 with a 404, file not found
  • takes that response from itself and returns the 404 over port 80 to your browser

Now, there’s more than one way to fix this, depending on how your backend project is designed to work.

But the most obvious one is to use the without subdirective for proxy.

  • without is a URL prefix to trim before proxying the request upstream. A request to /api/foo without /api, for example, will result in a proxy request to /foo.

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

So using without /project-one would result in Caddy making its upstream connection directly to localhost:7000/ instead of requesting localhost:7000/project-one.

1 Like