9

解决Nginx配置http2不生效,谷歌浏览器仍然采用http1.1协议问题

 3 years ago
source link: https://zhang.ge/5114.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
WEB应用

解决Nginx配置http2不生效,谷歌浏览器仍然采用http1.1协议问题

Jager · 11月11日 · 2016年https协议 · nginx · nginx配置 · SSL证书 644次已读

昨天一个网友通过QQ联系我,说按照我博客之前分享的http2配置教程不能生效,想请我帮忙看看。

经过测试,使用谷歌浏览器访问他的测试站点,确实没有开启http2,但他的配置和编译参数都正确的,这有点奇怪了。

不过昨天太忙就没有继续帮他分析,他只好将服务器账号和密码都留言给了我。今天中午我抽空在他服务器重新编译测试了一把,才发现原来是这么一个梗!

他在编译Nginx之前,使用的是yum安装的openssl,可能是他的yum源太陈旧,或者没配置EPEL导致yum安装的openssl版本过低!而他在编译Nginx的时候并没有使用--with-openssl=DIR的选项来静态编译,所以他编出来的Nginx用的系统低版本的openssl,导致谷歌访问时并不会开启http2!

找了段专业解释如下:

Chrome 在最近的更新中放弃了对 NPN 的支持,如果想要继续在 Chrome 上支持 HTTP/2 ,则需要安装最新 1.0.2 版的 OpenSSL,并且用 1.0.2 的 OpenSSL 重新编译 Nginx。

参考资料:

新版Chrome下滚回HTTP/1.1

Supporting HTTP/2 for Google Chrome Users

所以,解决方法就非常简单了,从openssl官网下载最新源码包,然后新增如下参数重新编译即可:

--with-openssl=源码包解压目录

./configure --prefix=/usr/local/nginx --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-openssl=/data/openssl-1.0.2c

当然,我们也可以先更新yum源,比如改用EPEL源,使用 yum update openssl 升级后重新编译。这里我个人建议使用源码静态编译。

重新编译安装后,再利用谷歌浏览器访问如下网址:

chrome://net-internals/#http2

测试他的网站已经成功开启http2了:

事后突然想起,其实自己之前折腾网站的时候其实遇到过同样的问题,就因为没有记录导致重复造轮子。所以这次记录分享一下,权当是备忘吧!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK