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

Response compression requires the PHP zlib extension.

Usage Example

server.php
<?php

require __DIR__ . '/vendor/autoload.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\HttpServerWorker;

$server = new Server();

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

$server->addWorker(
new HttpServerWorker(
// HttpServerWorker 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, HttpServerWorker $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

ParameterTypeDefaultDescription
http2EnabledbooltrueOptional. 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 body length required for compression when gzip is enabled.
gzipTypesRegexstringSee below *Optional. Regular expression that determines which response Content-Type values are compressed.

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

Worker Configuration

⚙️ HttpServerWorker

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

ParameterTypeDefaultDescription
listenstring|Listen|string[]|Listen[]requiredAddress or addresses on which to listen.
namestringHTTP ServerOptional. Worker name. Defaults to HTTP Server.
countint|nullnullOptional. Number of worker processes. Must be between 1 and 1024. Defaults to the detected CPU count.
reloadablebool|truetrueOptional. Whether the worker can be reloaded.
userstring|nullnullOptional. Unix user name or user ID (UID).
groupstring|nullnullOptional. Unix group name or group ID (GID).
onStartClosure|nullnullOptional. Closure(HttpServerWorker): void callback executed when the worker starts.
onRequestClosure|nullnullOptional. Closure(Request, HttpServerWorker): Response. HTTP request handler.
onStopClosure|nullnullOptional. Closure(HttpServerWorker): void callback executed when the worker stops.
onReloadClosure|nullnullOptional. Closure(HttpServerWorker): void callback executed when the worker reloads.
middlewareMiddleware[][]Optional. Additional HTTP middleware.
reloadStrategiesReloadStrategy[][]Optional. Strategies for triggering automatic worker reloads.
documentRootstring|nullnullOptional. Directory from which to serve static files.
accessLogbooltrueOptional. Whether to log incoming HTTP requests.
gzipboolfalseOptional. Enables response compression. Requires ext-zlib.
connectionLimitint|nullnullOptional. Maximum number of simultaneous connections per worker. Disabled by default.
connectionLimitPerIpint|nullnullOptional. Maximum number of simultaneous connections per IPv4 address or IPv6 /56 block. Loopback addresses are exempt.
concurrencyLimitint|nullnullOptional. Maximum number of concurrent requests per worker. Disabled by default.

Reload Strategies

🔄️ EachRequestReloadStrategy

Reload strategy class: EachRequestReloadStrategy
This strategy requests a worker reload after every HTTP request, which makes it primarily useful for debugging purposes.

🔄️ MaxRequestsReloadStrategy

Reload strategy class: MaxRequestsReloadStrategy
This strategy reloads the worker when its request count reaches the configured threshold.

ParameterTypeDefaultDescription
maxRequestsintrequiredThe maximum number of requests a worker can handle before being reloaded.
dispersionPercentageint0Optional. Percentage by which the request threshold may be reduced.

Parameter dispersionPercentage adds variability to the reload threshold to prevent all workers from restarting at the same time. For example, with maxRequests: 1000 and dispersionPercentage: 20, the threshold is set between 800 and 1000 requests.