Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Swoole HttpServer instance


May 14, 2021 Swoole


Table of contents


About Swoole HttpServer

swoole-1.7.7 adds support for the built-in Http server, which can be written out of an asynchronous non-blocking multi-process http server in a few lines of code.

Common use scenarios: Because swoole is executed under the cli command, many root shells cannot be executed in the traditional nginx-fastcgi mode, and using this swoole server is good for controlling rsync, git, svn, etc.

$http = new swoole_http_server("127.0.0.1", 9501);
$http->on('request', function ($request, $response) {
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();

swoole_http_server support for the Http protocol is incomplete and is recommended only as an application server. Nginx is added as a proxy at the front end

A simple example:

$serv = new Swoole\Http\Server("127.0.0.1", 9502);

$serv->on('Request', function($request, $response) {
    var_dump($request->get);
    var_dump($request->post);
    var_dump($request->cookie);
    var_dump($request->files);
    var_dump($request->header);
    var_dump($request->server);

    $response->cookie("User", "Swoole");
    $response->header("X-Server", "Swoole");
    $response->end("<h1>Hello Swoole!</h1>");
});

$serv->start();

Using the apache bench tool for stress testing, the swoole_http_server can reach nearly 110,000 QPS on a regular PC machine with Inter Core-I5 4 core and 8G memory. F ar more than php-fpm, golang brings its own http server, node.js brings its own http server. Performance is almost close to static file processing with Nginx.

ab -c 200 -n 200000 -k http://127.0.0.1:9501


Use the Http2 protocol

  • Need to rely nghttp2 library, download nghttp2 after compiling the installation
  • Openssl must be turned openssl Http2 protocol
  • High openssl required TLS1.2 ALPN NPN
./configure --enable-openssl --enable-http2

The setting of the open_http2_protocol open_http2_protocol is true

$serv->set([
    'ssl_cert_file' => $ssl_dir . '/ssl.crt',
    'ssl_key_file' => $ssl_dir . '/ssl.key',
    'open_http2_protocol' => true,
]);

nginx-swoole configuration

server {
    root /data/wwwroot/;
    server_name local.swoole.com;

    location / {
        if (!-e $request_filename) {
             proxy_pass http://127.0.0.1:9501;
             proxy_http_version 1.1;
             proxy_set_header Connection "keep-alive";
        }
    }
}


Asynchronous Http/WebSocket client

Version Swoole-1.8.0 adds support for asynchronous Http/WebSocket clients. The bottom layer is written in pure C and has very high performance.

Asynchronous HTTP clients are still in the experimental phase, please use them with caution

Enable the Http client

  • Before version 1.8.6, you need to add --enable-async-httpclient when compiling swoole to turn on this feature.
  • swoole_http_client does not rely on any third-party libraries
  • Http-Chunk, Keep-Alive features are supported, and form-data format is not supported at this time
  • The http protocol version is HTTP/1.1
  • Gzip compression format support relies on the zlib library

The construction method

function swoole_http_client->__construct(string $ip, int port, bool $ssl = false);
  • $ip the IP address of the target server, you can use swoole_async_dns_lookup to query the IP address of the domain name
  • $port the port of the target server, the typical http is 80 and https is 443
  • $ssl whether to enable SSL/TLS tunnel encryption, if the target server is https you must set the $ssl parameter to true