Nginx Http2 技术 Nginx 配置 http2 2017-10-20 16:50 2876 更新于 2017-10-20 16:50 越来越多的网站已经支持http2了,前几天配置了博客的https,顺便也开启了http2,这里做下记录。 先来看看http2的效果,很出名的一个demo https://http2.akamai.com/demo 更多理论知识,我也不再强调了,但这里还是贴一个 http2 的优化建议,https://www.w3ctech.com/topic/1563 若是没有配置 https ,可以参考之前的文章 OpenSSL 生成证书和Nginx的https配置 先配置好 https nginx 的支持http2模块是 ngx_http_v2_module,且该模块是nginx 1.9.5 才有的,所以,小伙伴们还是先升级nginx吧,还有openssl的版本也要大于 1.0.2。[官方文档](https://nginx.org/en/docs/http/ngx_http_v2_module.html "官方文档") 查看了我的nginx版本和模块,刚好都满足要求,哈哈 ``` nginx -V nginx version: nginx/1.12.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.2l 25 May 2017 TLS SNI support enabled configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/usr/local/openssl-1.0.2 ``` lhttp_v2_module,http_ssl_module 若缺少相应模块,就下载对应的模块源码 进入nginx源码目录进行编译 ``` ./configure --user=www --group=www --prefix=/usr/local/nginx --add-module=/模块目录/模块名称 之前的参数 ``` 然后就 make,**不要执行 make install** 在 ./objs 文件夹下生成一个 nginx 二进制文件 先备份原有的二进制文件(以防万一) ``` cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak ``` 替换原来的 nginx 文件 ``` cp /nginx源码目录/objs/nginx /usr/local/nginx/sbin/ ``` 重启 nginx 生效 (我这里模块都有,所以就不演示了) 然后在要开启 http2 的server 中添加 listen 443 ssl http2; 如 ``` server { listen 443 ssl http2; ssl on; ssl_certificate /etc/pki/CA/nginx.crt; ssl_certificate_key /etc/pki/CA/nginx.key; server_name tests.jam00.com; index index.html index.htm index.php; root /mnt/hgfs/www/test1; } ``` 如何知晓访问的网站是否支持了 http2 呢,这里分享一个 chrome 的插件 [HTTP2_and_SPDY_indicator](https://download.csdn.net/download/jam00/10031983 "HTTP2_and_SPDY_indicator") 拖拽安装即可