Nginx如何实现支持http2

1,运行环境

OS:CenOS6
Nginx:Tengine 2.1.2或者其他nginx官方版本
openssl下载:
https://www.openssl.org/source/old/1.0.2/openssl-1.0.2k.tar.gz

2,http2要求

2.1,openssl版本

最低要求openssl 1.0.2

2.2,nginx编译http2模块

http2需要nginx安装支持with-http_v2_module模块

3,升级openssl

CentOS6默认openssl版本是2013年发布的1.0.1,这里,我们需要升级到2017发布的openssl 1.0.2版本。

$ sudo tar zxf openssl-1.0.2k.tar.gz
$ sudo cd openssl-1.0.2k/ && ./config && make && make install
$ sudo mv /usr/bin/openssl /usr/bin/openssl.OFF
$ sudo mv /usr/include/openssl /usr/include/openssl.OFF

$ sudo ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
$ sudo ln -s /usr/local/ssl/include/openssl /usr/include/openssl

$ openssl version -a
OpenSSL 1.0.2k  26 Jan 2017
built on: reproducible build, date unspecified
platform: linux-elf
options:  bn(64,32) rc4(8x,mmx) des(ptr,risc1,16,long) idea(int) blowfish(idx) 
compiler: gcc -I. -I.. -I../include  -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -Wa,--noexecstack -DL_ENDIAN -O3 -fomit-frame-pointer -Wall -DOPENSSL_BN_ASM_PART_WORDS -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_GF2m -DRC4_ASM -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DRMD160_ASM -DAES_ASM -DVPAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM
OPENSSLDIR: "/usr/local/ssl"

4,编译安装nginx

$ sudo ./configure --prefix=/usr/local/tengine-2.1.2 --with-http_ssl_module --with-http_stub_status_module --with-http_spdy_module --with-http_v2_module --with-http_gzip_static_module --with-http_concat_module --with-openssl=/usr/local/ssl --user=www --group=www
$ sudo make && make install

注意:with-openssl选项指定openssl的安装路径。

5,编译安装nginx失败

5.1,make失败,找不到openssl

	objs/src/http/modules/ngx_http_upstream_session_sticky_module.o \
	objs/src/http/modules/ngx_http_reqstat_module.o \
	objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
	objs/src/http/modules/ngx_http_upstream_dynamic_module.o \
	objs/src/http/modules/ngx_http_stub_status_module.o \
	objs/ngx_modules.o \
	-lpthread -ldl -lcrypt -lpcre -lssl -lcrypto -ldl -lz
objs/src/http/ngx_http_request.o: In function `ngx_http_ssl_handshake_handler':
/root/source/tengine-2.1.2/src/http/ngx_http_request.c:806: undefined reference to `SSL_get0_alpn_selected'
objs/src/http/modules/ngx_http_ssl_module.o: In function `ngx_http_ssl_merge_srv_conf':
/root/source/tengine-2.1.2/src/http/modules/ngx_http_ssl_module.c:688: undefined reference to `SSL_CTX_set_alpn_select_cb'
collect2: ld returned 1 exit status
make[1]: *** [objs/nginx] Error 1
make[1]: Leaving directory `/root/source/tengine-2.1.2'
make: *** [install] Error 2

解决方案

主要原因是,升级openssl后默认的ssl PATH发生变化导致。
这里,文件auto/lib/openssl/conf中

             CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
             CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
             CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
             CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
             CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

修改为

            CORE_INCS="$CORE_INCS $OPENSSL/include"
            CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
            CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
            CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
            CORE_LIBS="$CORE_LIBS $NGX_LIBDL"

然后,重新执行编译安装即可。

6,检查nginx安装成功

$ sudo /usr/local/nginx/sbin/nginx -V
Tengine version: Tengine/2.1.2 (nginx/1.6.2)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr/local/tengine-2.1.2 --with-http_ssl_module --with-http_stub_status_module --with-http_spdy_module --with-http_v2_module --with-http_gzip_static_module --with-http_concat_module --with-openssl=/usr/local/ssl --user=www --group=www

6,开启nginx的http2支持

listen 443 ssl http2 spdy default_server;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers   on;

7,补充知识

7.1,with-http_spdy_module模块

这里的编译安装,同时增加了with-http_spdy_module模块。并且,在nginx配置中开启了spdy协议。

7.2,spdy是什么

它是Google开发的基于TCP的会话层协议,主要功能:
最小化网络延迟,提升网络速度,优化用户的网络使用体验,是对http协议的增强。
实现数据流的多路复用、请求优化以及HTTP包头压缩。
参考资料:
https://baike.baidu.com/item/SPDY/3399551?fr=aladdin

7.3,nginx对spdy的支持

nginx官方 1.5.10开始正式支持spdy协议。
nginx官方 1.9.5开始,spdy模块的开启,被with-http_v2_module包含在内。
参考资料:
http://nginx.org/en/docs/http/ngx_http_spdy_module.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值