背景说明:
系统默认curl一般不包含http2模块,需要自己编译添加
环境背景:
CentOS Linux release 7.6.1810 (Core)
一、查看环境依赖
1.查看系统环境
cat /etc/redhat-release
2.查看openssl版本
openssl version
注:如果没有,或者版本在1.0.1及以下则需要安装/更新
安装:yum install openssl openssl-devel -y
更新:yum update openssl openssl-devel -y
3.查看curl版本
curl -V
注:如果包含http2就是curl已经支持http2,否则需要配置,如:
二、安装依赖
1.安装编译工具
yum groupinstall "Development Tools"
2.编译安装nghttp2-devel
git clone https://github.com/tatsuhiro-t/nghttp2.git
cd nghttp2
autoreconf -i
./configure
make
make install
echo '/usr/local/lib' > /etc/ld.so.conf.d/custom-libs.conf
ldconfig
ldconfig -p| grep libnghttp2
注意:
- 只需要使用nghttp2时可以直接安装nghttp2,如yum install nghttp2 -y,需要编译curl时,则需要编译nghttp2
- 报系统访问不到https://github.com/时,可以尝试一下nslookup github.com,然后ping 解析出来的那个网站ip,就是测一下疏通一下。再git就能通了(其实看人品)
三、编译安装curl
1.下载curl最新版源码
https://curl.se/download.html
注:选一个最新的下载,我这里下的是8.5.0版本
2.导入服务器,然后安装
tar -zxf curl-8.5.0.tar.gz
cd curl-8.5.0
./configure --prefix=/usr/local/curl --with-nghttp2 --with-openssl
make && make install
注:
- –prefix是指定curl安装的位置
- –with-nghttp2编译curl时附加nghttp2模块
- –with-openssl编译curl时附加openssl模块,使用http2协议时,需要使用ssl加密。
3.校验是否成功
/usr/local/curl/bin/curl -V
注:如果包含http2模块时,则证明安装成功,如:
4.配置成系统默认curl
export PATH="/usr/local/curl/bin:$PATH"
source ~/.bashrc
ln -sf /usr/local/curl/bin/curl /usr/bin/curl
注:
- export PATH 配置环境变量
- source ~/.bashrc 刷新环境变量配置
- ln -sf 将系统默认的curl符号链接指向新编译的curl二进制文件
四、使用curl校验http2
curl --http2 -I https://www.example.cn -k
注:
- –http2访问http2协议的网站,必须加上,不然会默认使用http1访问
- -k 取消ssl证书校验,当网站使用自签证书时适用
五、附加一个nginx的http2.0配置
1.nginx模块
注:必须要带有with-http_ssl_module和with-http_v2_module
2.nginx.conf配置
六、附加一个chrome查看网站http2协议
1.打卡调式模式的控制台
f12–>Console
2.输入命令查看
window.chrome.loadTimes ()
注:
- 查看npnNegotiatedProtocol
- 显示h2时表明是http2