Swoole HTTP Server

The Swoole\HTTP\Server class inherits from the class Swoole\Server. It is a complete implementation of HTTP server.





$http = new Swoole\HTTP\Server("", 9501);

$http->on('request', function ($request, $response) {
    $response->end("<h1>Hello World. #".rand(1000, 9999)."</h1>");



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

HTTP2 protocol support

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

Swoole HTTP Server configuration:

// Enable SSL
$server = new Swoole\HTTP\Server("", 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);
// setup the location of ssl cert files and key files
    'ssl_cert_file' => $ssl_dir . '/ssl.crt',
    'ssl_key_file' => $ssl_dir . '/ssl.key',
    'open_http2_protocol' => true, // Enable HTTP2 protocol

Nginx configuration:

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) {