dev-zuo 技术日常
Github

HTTP/2,怎么确定网站是否开启了HTTP/2,HTTP/3?

这篇文章发布于 2020/11/23,归类于
标签:
HTTP2HTTP3怎么开启HTTP/2

HTTP/2 是新一代的 HTTP 协议,于 2015 正式发布。相对 HTTP/1 来说,大幅提升了网页性能,绝大多数浏览器都支持了 HTTP/2。

http 怎么开启 http2 呢?HTTP/2 现阶段必须使用 https,80端口就不要想了。参考: 拥抱HTTP2.0时代,让网站飞起来 | 百度站长

HTTP/1.1 不足的地方

HTTP/2 就是为了解决 HTTP/1 存在的问题而产生的

开启 HTTP/2 只需要在 listen 443 ssl 后面加上 http2 即可,可以使用 curl -I 进行测试看HTTP/2是否生效

# /etc/nginx/conf.d/docker.conf 
server
{
    server_name www.zuoguoqing.com;

    location / {
      proxy_pass http://127.0.0.1:3000;
    }

    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.zuoguoqing.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.zuoguoqing.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

来看看 HTTP/1.1 和 HTTP/2 的测试对比

curl -I www.zuo11.com
# HTTP/1.1 200 OK
# Server: nginx/1.16.1
# Date: Thu, 08 Oct 2020 09:08:55 GMT
# Content-Type: text/html; charset=utf-8
# Content-Length: 3666
# Last-Modified: Thu, 01 Oct 2020 15:02:42 GMT
# Connection: keep-alive
# Vary: Accept-Encoding
# ETag: "5f75ef92-e52"
# Accept-Ranges: bytes

curl -I https://www.zuoguoqing.com
# HTTP/2 200 
# server: nginx/1.14.0 (Ubuntu)
# date: Thu, 08 Oct 2020 09:09:07 GMT
# content-type: text/html
# content-length: 213
# last-modified: Wed, 07 Oct 2020 09:03:03 GMT
# etag: "5f7d8447-d5"
# accept-ranges: bytes

HTTP/2够好了,为什么还会有 HTTP/3?

HTTP/2 的问题在于,其底层支撑协议为 TCP,在丢包的情况下,多个请求复用一个 TCP 连接时,整个 TCP 都要开始等待重传,也就导致了后面的所有数据都被阻塞了。这时 HTTP/2 效果可能还不如 HTTP/1

因此,Google 又弄了一个基于 UDP 协议的 QUIC 协议,是 HTTP/3 中的底层支撑协议,又取了 TCP 中的精华,实现了即快又可靠的协议。

参考