Home > Operation and Maintenance > Nginx > How to use Nginx to implement HTTP/2 protocol support

How to use Nginx to implement HTTP/2 protocol support

WBOY
Release: 2023-08-03 17:40:53
Original
1564 people have browsed it

How to use Nginx to implement HTTP/2 protocol support

Overview:
HTTP/2 is a new generation of HTTP protocol. Compared with the previous HTTP/1.x protocol, it has better performance and The efficiency has been greatly improved. In order to support the HTTP/2 protocol, we can use Nginx as the HTTP server and configure the corresponding settings.

Installing and configuring Nginx:
First, we need to install Nginx. It can be installed in the Ubuntu system through the following command:

sudo apt-get update
sudo apt-get install nginx
Copy after login

After the installation is complete, we need to configure Nginx to support the HTTP/2 protocol. Open the Nginx configuration file, which can be found at the following location:

sudo nano /etc/nginx/nginx.conf
Copy after login

Find the listen directive in the server block and add http2 as a parameter, as shown below:

server {
    listen 443 ssl http2;
    ...
}
Copy after login

Note, this assumes that your website uses an SSL certificate and uses the default port 443. If your website does not have SSL enabled or uses a different port, please adjust the configuration accordingly.

After saving and closing the file, restart Nginx for the changes to take effect:

sudo systemctl restart nginx
Copy after login

Configure the SSL certificate:
In order to use the HTTP/2 protocol, we need to configure the SSL certificate. In this example we will use a self-signed certificate. In a production environment, a certificate signed by a trusted certification authority should be used.

First, create a directory for storing SSL certificates:

sudo mkdir /etc/nginx/ssl
Copy after login

Then, generate a private key and certificate request:

sudo openssl req -new -newkey rsa:2048 -nodes -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.csr
Copy after login

Follow the prompts to fill in the relevant information, such as Country code, organization name, etc.

Next, the self-signed certificate:

sudo openssl x509 -req -days 365 -in /etc/nginx/ssl/nginx.csr -signkey /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
Copy after login

Configure Nginx to use an SSL certificate. Open the Nginx configuration file and find the following line:

server {
    ...
    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    ...
}
Copy after login

Uncomment the listen directive and add the path to the SSL certificate and the path to the private key:

server {
    ...
    # SSL configuration
    #
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    ...
}
Copy after login

Save and After closing the file, restart Nginx for the changes to take effect.

Test HTTP/2 protocol support:
To verify whether the HTTP/2 protocol is successfully enabled, please open a browser and visit your website. In the browser's developer tools, look at the Network tab and you can see that the requested protocol is HTTP/2.

Code example:
The following is a simple Nginx configuration example that includes support for the HTTP/2 protocol:

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    location / {
        root /var/www/html;
        index index.html;
    }
}
Copy after login

Conclusion:
Support HTTP/2 by using Nginx configuration 2 protocol, we are able to improve the performance and efficiency of our website. By following the steps in this article, you can easily implement support for the HTTP/2 protocol on your website.

The above is an introduction and sample code on how to use Nginx to implement HTTP/2 protocol support. I hope it will be helpful to you!

The above is the detailed content of How to use Nginx to implement HTTP/2 protocol support. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template