Nginx 使用HTTP/2.0

1、查看是否拥有开启条件
nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
2、检查是否符合开启条件
  1. openssl的版本必须在1.0.2e及以上
  2. nginx的版本必须在1.9.5以上
  3. 需要添加–with-http_v2_module模块
  4. 网站需要支持https访问

经检查我的网站只是没有http_v2_module,其他都符合条件,ok开整

3、添加http_v2_module模块
  1. 进入之前下载并解压了的源码包目录,重新编译nginx
  2. 在 ./configure 中加入:–with-http_v2_module
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module

注意事项:需要在你原有的模块基础上追加 --with-http_v2_module 。否则你之前的就木有了;
输入之后回车,他就会巴拉巴拉执行了

[root@gdxztest nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
checking for OS
Linux 3.10.0-693.2.2.el7.x86_64 x86_64
checking for C compiler ... found
using GNU C compiler
gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
............
等待执行完成即可
  1. 输入命令make进行编译

这一步千万不能 make install ;不然会把之前已经安装的nginx 覆盖掉

[root@gdxztest nginx-1.14.2]# make
make -f objs/Makefile
make[1]: Entering directory `/ming/nginx-1.14.2'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I >src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I >src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_log.o \
src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I >src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_palloc.o \
src/core/ngx_palloc.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I >src/event/modules -I src/os/unix -I objs \
 -o objs/src/core/ngx_array.o \
 src/core/ngx_array.c
..........
等待执行完成即可
  1. 备份一下原来的启动脚本,以防出现意外
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.back
  1. 先把nginx停掉,再把新生成的nginx文件覆盖到原来的nginx
[root@gdxztest nginx-1.14.2]# cp objs/nginx /usr/local/nginx/sbin/nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

若出现Text file busy 说明nginx 没有停掉
执行nginx -s stop

[root@gdxztest ~]# nginx -s stop

先停掉nginx再覆盖即可,覆盖后执行启动nginx命令

[root@gdxztest ~]# nginx -c /usr/local/nginx/conf/nginx.conf
  1. 检查是否添加成功 nginx -V
[root@gdxztest nginx-1.14.2]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
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_v2_module模块

  1. 配置nginx.conf,在监听443端口后添加 http2 即可
server{
	listen 443 ssl http2;
	......
  1. 重启nginx,注意重启之前先nginx -t 养成好习惯
[root@gdxztest nginx-1.14.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@gdxztest nginx-1.14.2]# nginx -s reload

  1. 查看网站访问是否正常,是否已切换为http2
:authority:www.xxx.com
:method:GET
:path:/app/manager/getSystemTimeStamp
:scheme:https
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

出现冒号开头的header属性,则表示配置成功,恭喜你成功切换到http2

共勉!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值