HTTP/2优点
采用二进制格式传输数据,而非文本格式。二进制格式在协议的解析和优化扩展上带来更多的优势和可能。
对消息头进行压缩传输,能够节省消息头占用的网络的流量,而 HTTP 1.1 每次请求,都会携带大量冗余头信息,浪费了很多带宽资源,头压缩能够很好的解决该问题。
多路复用,就是多个请求都是通过一个 TCP 连接并发完成, HTTP 1.1 虽然通过 pipeline 也能并发请求,但是多个请求之间的响应会被阻塞的,所以 pipeline 至今也没有被普及应用,而 HTTP/2 做到了真正的并发请求,同时流还支持优先级和流量控制。
服务器推送,服务端能够更快的把资源推送给客户端,例如服务端可以主动把 JS 和 CSS 文件推送给客户端,而不需要客户端解析 HTML 再发送这些请求,当客户端需要的时候,它已经在客户端了。
HTTP/2站点的优势
- 提升网站访问速度。
- 降低服务器压力。
- 部分替代异步加载的使用。
- 保护网站安全。
nginx配置http2
默认编译的 Nginx 并不包含 h2 模块,我们需要加入参数来编译。Nginx 1.9 开发版及以上版本源码需要自己加入编译参数。
以下是平滑升级模块过程
最后使用源码自带升级命令:make upgrade自动完成平滑
根据需求添加编译模块,不过只需要执行到make 不要执行make install!
http2需要openss1.0.2e以上版本
下载openssl1
wget https://www.openssl.org/source/openssl-1.0.2m.tar.gz
这里查看我当前nginx编译参数1
2/usr/local/nginx/sbin/nginx -V
configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module
到nginx源码下重新编译nginx,解压openssl指定openssl源码路径 –with-openssl=../openssl-1.0.2m
加上 –with-http_realip_module –with-http_v2_module –with-openssl=../openssl-1.0.21
2
3./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_v2_module --with-openssl=../openssl-1.0.2m
#执行到make 不要执行make install
make #执行make 但不要执行make install
备份二进制文件1
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.back
期间nginx并不会停止服务
拷贝新编译的二进制文件1
cp objs/nginx /usr/local/nginx/sbin/
在源码目录下执行平滑重新加载配置命令1
2
3
4
5
6
7
8nginx-1.12.0]# make upgrade
usr/local/nginx/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
验证1
2
3
4
5
6nginx-1.12.0]#/usr/local/nginx/sbin/nginx -V
nginx version: Microsoft-IIS/1.12.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.2m 2 Nov 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_v2_module --with-openssl=../openssl-1.0.2m
http2demo:https://http2.akamai.com/demo