Home>Article>Web Front-end> Teach you step by step how to enable https and http2 for your site (with code)

Teach you step by step how to enable https and http2 for your site (with code)

奋力向前
奋力向前 forward
2021-09-01 11:09:18 6006browse

In the previous article "A brief analysis of the installation and use of nginx (Collection)", I introduced you to the installation and use of nginx. The following article will introduce to you how to enable https and http2 for the site. Let's see how to do it together.

Teach you step by step how to enable https and http2 for your site (with code)

This article mainly introduces how to enablehttpsandhttp2

# for the siteTeach you step by step how to enable https and http2 for your site (with code)

##This article mainly introduces how to enable

httpsandhttp2

to enable

httpsfor the site. Simply take this siteLinux nginxas an example

3 commands to

download the script

wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto

automatically associate to

nginx
$sudo ./certbot-auto --nginx ```sh ## 检查证书有没有过期

$sudo ./certbot-auto renew --dry-run

## 如果过期了就自动续订 ```sh $sudo ./certbot-auto renew

If the following error code is prompted

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)is because the configuration file ofnginxcontains Chinese characters, so change the Chinese to English That’s it

Or you can search it by yourself

grep -r -P '[^\x00-\x7f]' /etc/nginx /etc/letsencrypt

Okay, now open

nginxConfiguration and take a look

listen 443 ssl http2; ssl on; ssl_certificate /etc/letsencrypt/live/chuchur.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/chuchur.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; if ($scheme != "https") { return 301 https://$host$request_uri; } # managed by Certbot

Note that the nginx version is too low If it cannot be opened, you must first upgrade the nginx version, taking centos as an example

Modify the nginx of

/etc/yum.repos.d/nginx.repoWarehouse address

[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=0 enabled=1

If nginx has not been installed, directly execute yum install nginx. If it has been installed, you can upgrade it through yum clean all && yum update nginx.

It is recommended to stop

nginxViolent stopkillall nginx, then uninstallyum remove nginxand then reinstallyum install nginx

Ubuntu/Debian upgrade HTTP/2

First modify

aptwarehouse source/etc/apt/ For the address of source.list, add the following two lines of warehouse.

deb http://nginx.org/packages/mainline/debian/ codename nginx deb-src http://nginx.org/packages/mainline/debian/ codename nginx

Next execute

apt-get clean && apt-get install nginx

Okay, now the

httpsis successfully opened,443 sslThe one behindhttp2, that is, it is turned onhttp2

Finally restart nginx

nginx -s reload

Refresh the page and find a lock in front of the URL, which means

httpsis successfully opened

How to verify that

http2is turned on? Open the URL to be verified and wait until it is loaded. Then open a newtaband enterchrome://net-internals /#http2Enter

If you find the IP corresponding to your URL in the list, it seems that it has been successfully opened.

Of course, use Google Chrome

[End]

Recommended learning:

http video tutorial

The above is the detailed content of Teach you step by step how to enable https and http2 for your site (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:chuchur.com. If there is any infringement, please contact admin@php.cn delete