http2.0的特性
- 多路复用
- 头部压缩
- 服务端推送
- 新的二进制格式,基本单位为帧,例如HEAD帧,DATA帧
- 流优先级
nginx编译
./configure --with-http_v2_module --with-debug
make -j 8 && make install
编译安装后查看,可以看到nginx是1.18.0版本,且已支持http2.0
# ./nginx -V
nginx version: nginx/1.18.0
built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
configure arguments: --with-http_v2_module --with-debug
nginx配置文件
user root;
worker_processes 4;
error_log logs/debug.log debug;
events {
worker_connections 1024;
}
http {
#include mime.types;
#default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
server {
client_max_body_size 4G;
listen 80 http2; # 此处加上http2就好
server_name www.wawa.com;
root /data;
location ~ /*.mp4$ {
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
}
}
}
使用curl测试
检查curl是否支持http2.0协议
# curl -V
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
可以看到在Features行有输出HTTP2,则说明我使用的curl支持http2.0
那curl如何使用http2.0进行下载呢?查看curl的help帮助
# curl -help |egrep http
-0, --http1.0 Use HTTP 1.0
--http1.1 Use HTTP 1.1
--http2 Use HTTP 2
--http2-prior-knowledge Use HTTP 2 without HTTP/1.1 Upgrade
则可以看到使用**--http2.0** 或者 --http2-prior-knowledge 即可。此处nginx仅仅支持http2.0,还不支持从http1.1协商协议升级到http2.0,所以暂时用 --http2-prior-knowledge下载,命令如下:
# curl -v --http2-prior-knowledge http://192.168.116.130:80/a.mp4 -o a1.mp4
* Trying 192.168.116.130...
* TCP_NODELAY set
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 192.168.116.130 (192.168.116.130) port 80 (#0)
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fffca8c5580)
> GET /a.mp4 HTTP/2 #已经使用http.20访问
> Host: 192.168.116.130
> User-Agent: curl/7.58.0
> Accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200 # 服务器响应也是http2.0协议
< server: nginx/1.18.0
< date: Sat, 05 Sep 2020 15:14:50 GMT
< content-type: text/plain
< content-length: 41780
< last-modified: Wed, 24 Jun 2020 12:11:53 GMT
< etag: "5ef34309-a334"
< accept-ranges: bytes
<
{ [13122 bytes data]
100 41780 100 41780 0 0 4080k 0 --:--:-- --:--:-- --:--:-- 4533k
* Connection #0 to host 192.168.116.130 left intact
http2.0 特性验证
头部压缩
可以看到对http request进行了头部压缩,当然也会对http response进行压缩,这里不在叙述。
协议其他特性需后续继续研究。