Home >PHP Framework >Workerman >Workerman development: How to implement a web server based on HTTP2 protocol
Workerman Development: How to implement a Web server based on the HTTP2 protocol
HTTP2 is a new generation version of the HTTP protocol, which has great improvements in performance and security improvement. Workerman is a commonly used PHP real-time communication framework, which has the advantages of high performance, easy expansion and ease of use. How to implement a web server based on HTTP2 protocol? This article will introduce the following aspects:
1. Understand the characteristics of the HTTP2 protocol
The HTTP2 protocol is a new generation version of the HTTP protocol. It has great improvements in performance and security. improvement. Compared with the HTTP1.x protocol, it has the following characteristics:
2. How Workerman supports the HTTP2 protocol
Workerman is a commonly used PHP real-time communication framework. It was originally designed to implement high-performance communication based on the TCP protocol, but it also Support HTTP protocol. Workerman uses HTTP1.x protocol by default, but it also supports HTTP2 protocol.
The basic condition for implementing the HTTP2 protocol is to have an SSL certificate, because the HTTP2 protocol only supports use in encryption mode. Therefore, we need to configure the SSL certificate in Workerman to support the HTTP2 protocol. The specific configuration method is as follows:
$context = array( // 这是key, 一般和crt放在一起 'ssl' => array( // 请使用绝对路径 'local_cert' => '/your/path/to/server.crt', // 服务端证书 'local_pk' => '/your/path/to/server.key', // 服务端证书的私钥 'verify_peer' => false, // 是否需要验证客户端证书 ) ); // 初始化一个Worker监听http://0.0.0.0:443 $worker = new Worker("http://0.0.0.0:443", $context); // 开启对HTTP2.0的支持 $worker->transport = 'ssl'; $worker->protocol = "Http2";
3. Implement the specific HTTP2 protocol Web server
After Workerman supports the HTTP2 protocol, we can implement the HTTP2 protocol Web server. There are many specific implementation methods. Here we take the implementation of a basic HTTP2 protocol Web server as an example.
<?php require_once __DIR__ . '/../vendor/autoload.php'; $context = array( 'ssl' => array( 'local_cert' => '/your/path/to/server.crt', 'local_pk' => '/your/path/to/server.key', 'verify_peer' => false, ) ); $worker = new WorkermanWorker('http://0.0.0.0:443', $context); $worker->transport = 'ssl'; $worker->protocol = "Http2"; $worker->onConnect = function($connection) { echo "new connection from ip " . $connection->getRemoteIp() . " "; }; $worker->onMessage = function($connection, $data) { $request_uri = $_SERVER['REQUEST_URI']; $response = "Hello, HTTP2! "; $connection->send($response); }; Worker::runAll();
Run The following command starts the web server:
php http2_server.php start -d
At this time, if you use a browser to access https://localhost, you should be able to see a page with the content Hello, HTTP2!.
4. Code Example
The code has been given in the third part, and the complete code is given again here. You only need to replace /your/path/to/server.crt and /your/path/to/server.key with your own SSL certificate path.
<?php require_once __DIR__ . '/../vendor/autoload.php'; $context = array( 'ssl' => array( 'local_cert' => '/your/path/to/server.crt', 'local_pk' => '/your/path/to/server.key', 'verify_peer' => false, ) ); $worker = new WorkermanWorker('http://0.0.0.0:443', $context); $worker->transport = 'ssl'; $worker->protocol = "Http2"; $worker->onConnect = function($connection) { echo "new connection from ip " . $connection->getRemoteIp() . " "; }; $worker->onMessage = function($connection, $data) { $request_uri = $_SERVER['REQUEST_URI']; $response = "Hello, HTTP2! "; $connection->send($response); }; Worker::runAll();
Summary
The HTTP2 protocol is a new generation version of the HTTP protocol. Compared with the HTTP1.x protocol, it has been greatly improved in terms of performance and security. Workerman is a commonly used PHP real-time communication framework that supports HTTP2 protocol. This article describes how to use Workerman to implement a web server based on the HTTP2 protocol, including configuring an SSL certificate and implementing a specific web server.
The above is the detailed content of Workerman development: How to implement a web server based on HTTP2 protocol. For more information, please follow other related articles on the PHP Chinese website!