caddy-php-fastcgi-connlimit 
This Caddy module limits the number of concurrent connections made by a
php_fastcgi handler. Requests wait for a free slot instead of being rejected.
If a request is canceled while waiting, it is removed from the queue.
The limit applies only to requests that reach the generated FastCGI reverse
proxy. Static files and other handlers are not limited.
Caddyfile
Use php_fastcgi_connlimit in place of php_fastcgi. The first argument after
an optional matcher is the connection limit. The remaining syntax is the same
as Caddy's php_fastcgi directive.
wiki.gslin.org {
root * /srv/wiki
php_fastcgi_connlimit 1 unix//run/php/php8.4-fpm.sock
file_server
}
All standard php_fastcgi subdirectives are passed to Caddy's own parser:
wiki.gslin.org {
root * /srv/wiki
php_fastcgi_connlimit 2 unix//run/php/php8.4-fpm.sock {
capture_stderr
env APP_ENV production
}
file_server
}
The standard directive can also be nested when its original layout is useful:
wiki.gslin.org {
php_fastcgi_connlimit 1 {
php_fastcgi unix//run/php/php8.4-fpm.sock
}
}
Syntax:
php_fastcgi_connlimit [<matcher>] <max_connections> <upstreams...> {
<php_fastcgi subdirectives...>
}
The limit is shared by all upstreams declared in one directive. Separate
directive instances have separate limits.
Why two adjacent directives are not supported
This form cannot precisely limit only FastCGI connections through Caddy's
public module API:
php_fastcgi unix//run/php/php8.4-fpm.sock
php_fastcgi_connlimit 1
php_fastcgi expands into a self-contained subroute. A sibling middleware can
run before or after that subroute, but it cannot be inserted into it. Running
before it would also limit static requests, while running after it would not see
PHP requests. This module instead asks Caddy's standard php_fastcgi parser to
build the subroute and inserts the limiter immediately before its FastCGI
reverse proxy.
Build
For local development:
go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
xcaddy build v2.11.4 \
--with github.com/gslin/caddy-php-fastcgi-connlimit=.
After the module is published, omit the local replacement:
xcaddy build v2.11.4 \
--with github.com/gslin/caddy-php-fastcgi-connlimit
Test
go test -race ./...
The current code and tests target Caddy v2.11.4.
Development disclosure
This project was developed with assistance from OpenAI Codex, a large language
model (LLM). All assisted changes were reviewed and tested by the maintainer.
License
MIT