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、检查是否符合开启条件
- openssl的版本必须在1.0.2e及以上
- nginx的版本必须在1.9.5以上
- 需要添加–with-http_v2_module模块
- 网站需要支持https访问
经检查我的网站只是没有http_v2_module,其他都符合条件,ok开整
3、添加http_v2_module模块
- 进入之前下载并解压了的源码包目录,重新编译nginx
- 在 ./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 ............ 等待执行完成即可
- 输入命令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 .......... 等待执行完成即可
- 备份一下原来的启动脚本,以防出现意外
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.back
- 先把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
- 检查是否添加成功 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模块
- 配置nginx.conf,在监听443端口后添加 http2 即可
server{
listen 443 ssl http2;
......
- 重启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
- 查看网站访问是否正常,是否已切换为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
共勉!