May 14, 2021 Swoole
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
$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
nghttp2
library,
download nghttp2 after
compiling the installation
openssl
Http2
protocol
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,
]);
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";
}
}
}
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
function swoole_http_client->__construct(string $ip, int port, bool $ssl = false);