Skip to main content

Http Server Plugin

The HTTP Server Plugin provides an asynchronous HTTP server and includes additional reload strategies.

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\HttpServerProcess;
use PHPStreamServer\Plugin\HttpServer\ReloadStrategy\MaxRequestsReloadStrategy;

$server = new Server();

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

$server->addWorker(
new HttpServerProcess(
// HttpServerProcess configuration
name: 'Web Server',
count: 2,
listen: '0.0.0.0:8080',
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());

Configuration

🔌 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 eligible for gzip compression.

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

⚙️ HttpServerProcess

This worker type is designed to handle 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.
countint1Optional. The number of processes to start.
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.

🔄️ EachRequestReloadStrategy

This strategy reloads the worker after every HTTP request, making it primarily useful for debugging purposes.

🔄️ MaxRequestsReloadStrategy

This strategy reloads the worker after handling 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 avoid restarting all workers simultaneously. For example, with maxRequests = 1000 and dispersionPercentage = 20, workers will be reloaded after handling between 800 and 1000 requests.