The Swoole\HTTP\Server
class inherits from the class Swoole\Server
. It is a complete implementation of HTTP server.
Swoole\HTTP\Server::__construct
Swoole\HTTP\Server->set
Swoole\HTTP\Server->on
Swoole\HTTP\Server->listen
Swoole\HTTP\Server->addListener
Swoole\HTTP\Server->addProcess
Swoole\HTTP\Server->start
Swoole\HTTP\Server->reload
Swoole\HTTP\Server->stop
Swoole\HTTP\Server->shutdown
Swoole\HTTP\Server->tick
Swoole\HTTP\Server->after
Swoole\HTTP\Server->defer
Swoole\HTTP\Server->close
Swoole\HTTP\Server->send
Swoole\HTTP\Server->sendfile
Swoole\HTTP\Server->sendto
Swoole\HTTP\Server->sendwait
Swoole\HTTP\Server->sendMessage
Swoole\HTTP\Server->exist
Swoole\HTTP\Server->pause
Swoole\HTTP\Server->resume
Swoole\HTTP\Server->getClientInfo
Swoole\HTTP\Server->getClientList
Swoole\HTTP\Server->bind
Swoole\HTTP\Server->stats
Swoole\HTTP\Server->task
Swoole\HTTP\Server->taskCo
Swoole\HTTP\Server->taskWait
Swoole\HTTP\Server->taskWaitMulti
Swoole\HTTP\Server->finish
Swoole\HTTP\Server->heartbeat
Swoole\HTTP\Server->getLastError
Swoole\HTTP\Server->getSocket
Swoole\HTTP\Server->protect
Swoole\HTTP\Server->getCallback
Swoole\HTTP\Server->getReceivedTime
Swoole\HTTP\Server->getWorkerId
Swoole\HTTP\Server->getWorkerPid
Swoole\HTTP\Server->getManagerPid
Swoole\HTTP\Server->getMasterPid
Swoole\HTTP\Server->on('start', fn)
Swoole\HTTP\Server->on('shutdown', fn)
Swoole\HTTP\Server->on('workerstart', fn)
Swoole\HTTP\Server->on('workerstop', fn)
Swoole\HTTP\Server->on('timer', fn)
Swoole\HTTP\Server->on('packet', fn)
Swoole\HTTP\Server->on('close', fn)
Swoole\HTTP\Server->on('task', fn)
Swoole\HTTP\Server->on('finish', fn)
Swoole\HTTP\Server->on('pipemessage', fn)
Swoole\HTTP\Server->on('workererror', fn)
Swoole\HTTP\Server->on('managerstart', fn)
Swoole\HTTP\Server->on('managerstop', fn)
Swoole\HTTP\Server->on('beforereload', fn)
Swoole\HTTP\Server->on('afterreload', fn)
Swoole\HTTP\Server->on('request', fn)
Swoole\HTTP\Request
Swoole\HTTP\Response
<?php
$http = new Swoole\HTTP\Server("127.0.0.1", 9501);
$http->on('request', function ($request, $response) {
$response->end("<h1>Hello World. #".rand(1000, 9999)."</h1>");
});
$http->start();
Compare with PHP-FPM, the default Golang HTTP server, the default Node.js HTTP server, Swoole HTTP server performs much better. It has the similar performance compare with the Nginx static files server.
The experiment is done with benchmark tool Apache bench, on a normal PC server with Inter Core-I5 4 core CPU, 8GB memory. Swoole HTTP server hits 110K request per second.
ab -c 200 -n 200000 -k http://127.0.0.1:9501
Swoole HTTP server supports HTTP2 protocol thanks to nghttp2 library. Openssl is required for HTTP2 protocol and the openssl has to support TLS1.2, ALPN and NPN.
PHP extension compile configuration:
./configure --enable-openssl --enable-http2
<?php
// Enable SSL
$server = new Swoole\HTTP\Server("127.0.0.1", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
// setup the location of ssl cert files and key files
$server->set([
'ssl_cert_file' => $ssl_dir . '/ssl.crt',
'ssl_key_file' => $ssl_dir . '/ssl.key',
'open_http2_protocol' => true, // Enable HTTP2 protocol
]);
server {
root /data/wwwroot/;
server_name localhost;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
proxy_set_header X-Real-IP $remote_addr;
if (!-e $request_filename) {
proxy_pass http://127.0.0.1:9501;
}
}
}