从零开始搭建支持http2的web服务

  前段时间开始,公司各项业务开始陆续接入http2,关于http2的优点与所适用的场景网上有很多的文档可以查阅,这里我主要是总结分享一下如何从0到1搭建http2服务。

  这里先说明一下,要完成http2的请求需要客户端和服务端同时支持,如下表格可以看出,只要客户端或服务端任意一端不支持http2,都会自动降级到http1.1

  

 

  一、以下为客户端(各浏览器)支持情况

  

  目前除了Opera Mini以及UC Browser for Android 以外,其他浏览器支持情况还算不错,可是这些支持http2的浏览器也是有条件的,可以看到下图中下面的1、2、4,主要是2和4:必须是https以及服务端要支持ALPN(应用层协议协商);详细一点的服务端要求,请看下面。

 

  二、以下为服务端要求:

  google开发SPDY时提出了NPN(下一代协议协商),但随着SPDY被HTTP/2取代,google渐渐放弃了NPN,而改为ALPN(应用层协议协商);从chrome51开始就移除了对NPN的支持,只保留了支持ALPN(因此服务端必须要支持ALPN)

    1)OpenSSL 需要>=1.0.2的版本,因为也只有1.0.2或以上版本的OpenSSL才支持ALPN,而遗憾的是一般的Linux默认内置的OpenSSL(<1.0.2)都是不支持ALPN的;

    2)nginx 需要 >= 1.9.5,但据说在nginx 1.9.15 ~ 1.10.x有POST bug,然后在1.11.0才修复了这个BUG(详情请参阅:https://imququ.com/post/nginx-http2-post-bug.html

 

  三、让网站支持HTTP/2:

  好的,上面一和二说明了服务端与客户端的要求,只要满足了这些要求,就可以顺利搭建http2的服务;

  1、升级服务端:

    1)升级OpenSSL

下载openssl 1.1.0(https://www.openssl.org/source/openssl-1.1.0c.tar.gz)

解压:

[root@static-mwrhc ~]# tar -zvxf openssl-1.1.0c.tar.gz

安装:

[root@static-mwrhc ~]# cd openssl-1.1.0c

[root@static-mwrhc openssl-1.1.0c]# ./config shared zlib

[root@static-mwrhc openssl-1.1.0c]# make

[root@static-mwrhc openssl-1.1.0c]# make install

备份原有的openssl:

[root@static-mwrhc openssl-1.1.0c]# mv /usr/bin/openssl /usr/bin/openssl.back

[root@static-mwrhc openssl-1.1.0c]# mv /usr/include/openssl /usr/include/openssl.back

建立软链:

[root@static-mwrhc openssl-1.1.0c]# ln -s /usr/local/bin/openssl /usr/bin/openssl

[root@static-mwrhc openssl-1.1.0c]# ln -s /usr/local/include/openssl /usr/include/openssl

配置动态连接库:

[root@static-mwrhc openssl-1.1.0c]# echo "/usr/local/lib64/" >> /etc/ld.so.conf

[root@static-mwrhc openssl-1.1.0c]# ldconfig 

升级完成,查看openssl版本:

[root@static-mwrhc openssl-1.1.0c]# openssl version

OpenSSL 1.1.0c  10 Nov 2016

 

2)安装新版Nginx

安装pcre的devel依赖包:

[root@static-mwrhc nginx-1.13.9]# yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

下载 nginx-1.13.9.tar.gz(http://nginx.org/download/nginx-1.13.9.tar.gz)并解压:

[root@static-mwrhc ~]# tar -zvxf nginx-1.13.9.tar.gz

编绎:

[root@static-mwrhc ~]# cd nginx-1.13.9

[root@static-mwrhc nginx-1.13.9]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_flv_module --with-http_gzip_static_module --with-pcre --with-stream --with-openssl=/root/openssl-1.1.0c【openssl的源码路径】

[root@static-mwrhc nginx-1.13.9]# make

如果出现以下错误:

cc: /root/openssl-1.1.0c/lib/libssl.a: No such file or directory

cc: /root/openssl-1.1.0c/lib/libcrypto.a: No such file or directory

make[1]: *** [objs/nginx] Error 1

make[1]: Leaving directory `/root/nginx-1.13.9'

make: *** [build] Error 2

则:

[root@static-mwrhc nginx-1.13.9]# ln -s /usr/local/lib64/libssl.a /root/openssl-1.1.0c/lib/libssl.a

[root@static-mwrhc nginx-1.13.9]# ln -s /usr/local/lib64/libcrypto.a /root/openssl-1.1.0c/lib/libcrypto.a

然后再次执行:

[root@static-mwrhc nginx-1.13.9]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_flv_module --with-http_gzip_static_module --with-pcre --with-stream --with-openssl=/root/openssl-1.1.0c【openssl的源码路径】

[root@static-mwrhc nginx-1.13.9]# make

安装:

[root@static-mwrhc nginx-1.13.9]# make install

安装完成,查看安装信息:

[root@static-mwrhc nginx-1.13.9]# /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.13.9

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 

built with OpenSSL 1.1.0c  10 Nov 2016

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_flv_module --with-http_gzip_static_module --with-pcre --with-stream --with-openssl=/root/openssl-1.1.0c

3)配置Nginx

server {

  listen                         443 ssl http2;

  server_name            h2.static.com; 

  root                           /apps/dat/web/working/h2.static.com;

  index                        index.html;

  #SSL配置

  ssl                            on;

  ssl_certificate           /usr/local/nginx/conf.d/server.crt;

  ssl_certificate_key   /usr/local/nginx/conf.d/server.key;

}

2、访问站点(成功):

 

  

  四、前端调试工具:

1、新版chrome支持HTTP/2,如果想做启动HTTP2的前后对比,可以退出chrome浏览器后,使用如下命令启动Chrome,就可以禁用http2,这样就会自动降级成HTTP/1.1:

以mac为例,使用命令行工具,找到chrome的安装位置,然后:/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --disable-http2

2、抓包:

目前feddler不支持http2,使用feddler进行抓包,浏览器会自动降级为http1.1

而charles支持http2

   

posted @ 2018-04-20 11:00  学明  阅读(3032)  评论(1编辑  收藏  举报