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
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
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
Find the listen directive in the server block and add http2
as a parameter, as shown below:
server { listen 443 ssl http2; ... }
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
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
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
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
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; ... }
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; ... }
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; } }
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!