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 HTTP2 protocol

王林
王林Original
2023-11-07 11:25:571017browse

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
  2. How Workerman supports the HTTP2 protocol
  3. Web server that implements the specific HTTP2 protocol
  4. Code examples

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:

  1. Binary protocol: HTTP2 uses a binary protocol, while HTTP1.x uses a text protocol. Binary protocols parse and transfer data faster.
  2. Multiplexing: HTTP2 can transmit multiple requests and responses in parallel on the same connection. This reduces connection establishment and latency, improving the overall responsiveness of the website.
  3. Header compression: HTTP2 uses the HPACK algorithm to compress the headers of requests and responses, reducing the size of data transmission and improving performance.
  4. Server push: HTTP2 can actively push web page-related resource files to the client, reducing the number of client requests and improving the web page opening speed.

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.

  1. Create a PHP file, named http2_server.php, enter the following code:
<?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();
  1. Start the Web server

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn