How to configure caddy reverse proxy in local development?

1. The problem I’m having:

I’m using caddy reverse-proxy locally to speed up development against a single-threaded PHP app on Windows by running 5 instances of the app with caddy sitting in front.

While this works reasonably well, I’m seeing 2 issues:

  • When the upstream server responds with a 408 timeout status code, Caddy retries the request instead of forwarding that response to the browser
  • Given that the servers can only handle 1 request at a time, I’d like to use the least_conn load balancing strategy to make best use of having 5 instances. By default, caddy doesn’t seem to do this.

My pwsh script to start caddy looks like this:

$processes = @()
$ports = 8081..8085
$ports | ForEach-Object {
  $processes += Start-Process php -PassThru -ArgumentList "artisan serve --port $_" -NoNewWindow
}
$upstreams = ($ports | ForEach-Object { "--to=:$_" })
caddy reverse-proxy --from :8000 @upstreams
$processes | ForEach-Object { Stop-Process $_.Id }

I’ve tried creating a Caddyfile in the same directory, but either Caddy isn’t finding it or is silently rejecting my syntax. I’ve also tried configuring via the CLI but if the options are available to do so I can’t find them.

2. Error messages and/or full log output:

3. Caddy version:

2.7.6

4. How I installed and ran Caddy:

I used winget install caddy to install. Script to run is pasted above.

a. System environment:

Windows 11

b. Command:

See above

c. Service/unit/compose file:

n/a

d. My complete Caddy config:

Trying to figure this out!

5. Links to relevant resources:

When you use the caddy reverse-proxy command, you’re not using a config file. Use caddy run to use a Caddyfile.

I strongly recommend using WSL instead to run PHP, so you’re not limited in this way. Then you can also use FrankenPHP which is a custom build of Caddy with PHP build in, which runs PHP directly. Laravel Octane has support for it.

That’s not a default behaviour of Caddy. Show evidence with curl -v and Caddy logs.

Right, you need to do that via Caddyfile config.

But like I said above, you’re trying to solve the wrong problem. Run PHP in WSL and you won’t need any of this.

1 Like

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