nginx配置域名启用http2协议

HTTP1.1则在1999年才开始广泛应用于现在的各大浏览器网络请求中,同时HTTP1.1也是当前使用最为广泛的HTTP协议。HTTP/2在支持HTTP1.1的基础上拓展了, 较之1.1在性能上有着大幅度的提升。现在基本都主流浏览器都支持HTTP2协议,许多支持HTTPS的网站基本也都启用了 HTTP2协议,若我们的域名配置了HTTPS证书,可以在nginx中配置启用HTTP2协议,加速网站响应效率。

 

一、HTTP 2.0简述

1、HTTP 2.0简介

HTTP/2 (原名HTTP/2.0)即超文本传输协议 2.0,是下一代HTTP协议。是由互联网工程任务组IETF)的Hypertext Transfer Protocol Bis (httpbis)工作小组进行开发。是自1999年http1.1发布后的首个更新。HTTP 2.0在2013年8月进行首次合作共事性测试。目前在开放互联网上HTTP 2.0将只用于https://网址,而http://网址将继续使用HTTP 1.1。

2、HTTP 2.0 优点

HTTP2.0大幅度的提高了web性能,在HTTP1.1完全语意兼容的基础上,进一步减少了网络的延迟。实现低延迟高吞吐量。对于前端开发者而言,减少了优化工作。有以下优点

  • 二进制分帧
  • 首部压缩
  • 流量控制
  • 多路复用
  • 请求优先级
  • 服务器推送

二、前提条件

1、openssl的版本必须在1.0.2e及以上,在 Chrome 51后,谷歌去掉了对 NPN 的支持,HTTP2不能用了会证书错误,而openssl1.0.2e之后的版本修复了此问题

2、开启https加密,目前http2.0只支持开启了https的网站

3、nginx的版本必须在1.9.5以上,原因是nginx从1.9.5开始,已经用 http_v2_module 模块替换了 ngx_http_spdy_module

三、准备openssl

1、查看openssl

$ openssl version 查看是否安装了openssl,安装了如下,且1.0.2k大于e

2、安装openssl

若没安装或者版本低于1.0.2e,去下载安装

下载 $ wget https://www.openssl.org/source/openssl-1.0.2r.tar.gz
解压 $ tar zxvf openssl-1.0.2r.tar.gz
进入 $ cd openssl-1.0.2r
配置 $ ./config
编译 $ make 
安装 $ make install

3、更新openssl

由于是源码编译安装,若之前安装过,openssl相关命令配置不会自动更新,需手动更新

备份旧的命令 

$ mv /usr/bin/openssl /usr/bin/openssl.old
$ mv /usr/include/openssl /usr/include/openssl.old

添加软连接
$ ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
$ ln -s /usr/local/ssl/include/openssl /usr/include/openssl

写入openssl库文件的搜索路径
$ echo "/usr/local/ssl/lib" >> /etc/ld.so.conf

使修改后的搜索路径生效 
$ ldconfig

重新查看openssl版本$ openssl version

若openssl已经安装且版本大于1.0.2e,以上操作可省略

四、修改nginx配置

1、查看nginx版本

$ /usr/local/openresty/nginx/sbin/nginx -V

nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 
--add-module=../ngx_devel_kit-0.3.0 --add-module=../iconv-nginx-module-0.14 
--add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 
--add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.32 
--add-module=../form-input-nginx-module-0.12 
--add-module=../encrypted-session-nginx-module-0.08 --add-module=../ngx_postgres-1.0 
--add-module=../srcache-nginx-module-0.31 
--add-module=../ngx_lua-0.10.13 --add-module=../ngx_lua_upstream-0.07 
--add-module=../headers-more-nginx-module-0.33 
--add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 
--add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.15 
--add-module=../rds-csv-nginx-module-0.09 
--add-module=../ngx_stream_lua-0.0.5 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib 
--with-http_realip_module 
--with-http_stub_status_module --with-http_ssl_module 
--with-http_gzip_static_module --with-stream --with-stream_ssl_module

看是否安装了with-http_v2_module,这里看到没有安装要安装

http_ssl_module看到已经安装了,若没有安装也要安装下

2、重新编译nginx

1、添加ngx_http_v2_module同时要带上原来的参数

$ ./configure --prefix=/usr/local/openresty --with-cc-opt=-O2 \
--with-http_realip_module \
--with-http_v2_module \
--with-http_stub_status_module \

--with-http_ssl_module \
--with-http_gzip_static_module \
--with-stream \
--with-stream_ssl_module \
--with-luajit

若要更新openssl加上

--with-openssl=/usr/local/ssl \ 

重新编译注意事项:

1、--prefix=/usr/local/openresty,是配置安装路径,而不是--prefix=/usr/local/openresty/nginx

2、要添加--with-luajit,重新配置luajit,不然启动nginx找不到libluajit-5.1.so.2,还得手动去添加

3、带有--add-module的模块无需带上,编译时openresty会自动添加

2、接下来$ make 编译,注意不要执行make install安装了,这样会覆盖原来的安装

3、备份原来的nginx二进制文件,以免出错时便于回退

$ cp /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx.bak

4、用新编译的nginx替换掉旧的nginx

$ cp  /opt/soft/openresty-1.13.6.1/build/nginx-1.13.6/objs/nginx  /usr/local/openresty/nginx/sbin/nginx

5、查看添加是否成功

$ /usr/local/openresty/nginx/sbin/nginx -V

nginx version: openresty/1.13.6.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx 
--with-cc-opt='-O2 -O2' 
--add-module=../ngx_devel_kit-0.3.0 
................. 
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib 
--with-http_realip_module 
--with-http_v2_module 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_gzip_static_module 
--with-stream 
--with-stream_ssl_module

可以看到 http_v2_module模块以已经添加成功

6、配置nginx.conf

 server {
 	listen       80;
        listen 443 ssl http2; #增加http2
        server_name www.uwsxxx.com;
        #开启ssl并添加证书
        ssl on;
        ssl_certificate cert/nginx_uws.pem;
        ssl_certificate_key cert/nginx_uws.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers on;
        
        access_log /usr/local/openresty/nginx/logs/access.log combined;
        
        location / {
                 proxy_set_header Host $host:$proxy_port;
                 proxy_set_header   X-Real-IP   $remote_addr;
                 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_pass http://uws02:7867;

                 proxy_redirect http:// $scheme://;
            	 if ( $server_port = 80 ){
                    rewrite ^(.*) https://$server_name$1 permanent;
             	 }

        }
}

7、重启nginx

$ /usr/local/openresty/bin/openresty -s reload 加载配置无效,必须停止后重新启动

必须先停止 $ /usr/local/openresty/bin/openresty -s stop

再重启启动 $ /usr/local/openresty/bin/openresty 

8、查看生效 

在chrome浏览器地址栏中输入我们的域名:https://www.uwscloud.com 访问,然后再输入chrome://net-internals/#http2 查看启用了http2的域名,可以看到配置的域名已经出现了

参考:http://nginx.org/en/docs/http/ngx_http_v2_module.html

参考:https://www.cnblogs.com/tinywan/p/7860774.html

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值