Skip to main content

Http Server Plugin

Asynchronous HTTP server with support for HTTP/2, HTTPS, static file serving, and Gzip compression.

Installation

$ composer require phpstreamserver/http-server

Example of Usage

server.php
use Amp\Http\Server\HttpErrorException;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use PHPStreamServer\Core\Server;
use PHPStreamServer\Plugin\HttpServer\HttpServerPlugin;
use PHPStreamServer\Plugin\HttpServer\ReloadStrategy\MaxRequestsReloadStrategy;
use PHPStreamServer\Plugin\HttpServer\Worker\HttpServerProcess;

$server = new Server();

$server->addPlugin(
new HttpServerPlugin(
// Optional. HttpServerPlugin configuration
),
);

$server->addWorker(
new HttpServerProcess(
// HttpServerProcess configuration
name: 'Web Server', // Worker name
listen: '0.0.0.0:8080', // Address to listen on
count: 2, // Number of worker processes
onRequest: function (Request $request, HttpServerProcess $worker): Response {
return match ($request->getUri()->getPath()) {
'/' => new Response(body: 'Hello world'),
'/ping' => new Response(body: 'pong'),
default => throw new HttpErrorException(404),
};
},
reloadStrategies: [
new MaxRequestsReloadStrategy(100),
],
),
);

exit($server->run());

Plugin Configuration

🧩 HttpServerPlugin

Plugin class: HttpServerPlugin

OptionTypeDefaultDescription
http2EnablebooltrueOptional. Enables support for HTTP/2 protocol.
httpConnectionTimeoutint60Optional. Timeout duration for idle HTTP connections.
httpHeaderSizeLimitint32768Optional. Maximum allowed size for HTTP headers.
httpBodySizeLimitint131072Optional. Maximum allowed size for the HTTP request body.
gzipMinLengthint860Optional. Minimum response size required to enable gzip compression.
gzipTypesRegexstring*Optional. Regular expression to match content types that will be gzipped.

* #^(?:text/.*+|[^/]*+/xml|[^+]*\+xml|application/(?:json|(?:x-)?javascript))$#i

Worker Configuration

⚙️ HttpServerProcess

Worker class: HttpServerProcess
This worker type handles incoming HTTP requests asynchronously.

OptionTypeDefaultDescription
listenstring|Listen|Listen[]not setThe address at which the server is listening.
namestring"HTTP Server"Optional. The name associated with the worker process.
countintnot setOptional. The number of processes to start. Defaults to number of CPUs.
reloadablebooltrueOptional. Whether the worker can be reloaded with the reload command.
userstringnot setOptional. Unix user of process. Current user by default.
groupstringnot setOptional. Unix group of process. Current group by default.
onStartClosurenot setOptional. A callback function executed when the worker starts.
onRequestClosurenot setOptional. A callback function executed when an HTTP request is received.
onStopClosurenot setOptional. A callback function executed when the worker stops.
onReloadClosurenot setOptional. A callback function executed when the worker reloads.
middlewareMiddleware[]not setOptional. A list of middlewares for processing HTTP requests.
reloadStrategiesReloadStrategy[]not setOptional. The strategies used to reload the worker.
serverDirstringnot setOptional. The directory to serve static files from.
accessLogbooltrueOptional. Whether to log incoming HTTP requests.
gzipboolfalseOptional. Enables gzip compression.
connectionLimitintnot setOptional. The maximum number of connections per worker.
connectionLimitPerIpintnot setOptional. The maximum number of connections allowed per IP.
concurrencyLimitintnot setOptional. The maximum number of concurrent HTTP requests per worker.

Reload Strategies

🔄️ EachRequestReloadStrategy

Reload strategy class: EachRequestReloadStrategy
This strategy reloads the worker after every HTTP request, which makes it primarily useful for debugging purposes.
It does not have any specific configurable options.

🔄️ MaxRequestsReloadStrategy

Reload strategy class: MaxRequestsReloadStrategy
This strategy reloads the worker after it has handled a specified number of HTTP requests.

OptionTypeDefaultDescription
maxRequestsintnot setThe maximum number of requests a worker can handle before being reloaded.
dispersionPercentageint0Optional. Variability percantage to maxRequests. *

* Adds variability to the reload threshold to prevent all workers from restarting at the same time. For example, with maxRequests = 1000 and dispersionPercentage = 20, workers will reload after handling between 800 and 1000 requests.