前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何通过Nginx服务器将系统平台升级至HTTP/2.0协议,实现访问速度的大幅提升

如何通过Nginx服务器将系统平台升级至HTTP/2.0协议,实现访问速度的大幅提升

作者头像
Tinywan
发布2023-11-22 14:45:59
3710
发布2023-11-22 14:45:59
举报
文章被收录于专栏:开源技术小栈开源技术小栈

HTTP/2.0 是什么

HTTP/2.0(超文本传输协议第2版,最初命名为HTTP 2.0),是HTTP协议的的第二个主要版本,使用于万维网。HTTP/2.0是HTTP协议自1999年HTTP 1.1发布后的首个更新,主要基于SPDY协议。它由互联网工程任务组(IETF)的Hypertext Transfer Protocol Bis(httpbis)工作小组进行开发。

该组织于2014年12月将HTTP/2标准提议递交至IESG进行讨论,于2015年2月17日被批准。HTTP/2标准于2015年5月以RFC 7540正式发表。

一、依赖

  • openssl的版本必须在1.0.2e及以上
  • nginx的版本必须在1.9.5以上

二、编译安装

1、openssl 编译安装

(1)开始

代码语言:javascript
复制
wget --no-check-certificate https://www.openssl.org/source/openssl-1.0.2j.tar.gz
tar zxvf openssl-1.0.2j.tar.gz
cd openssl-1.0.2j
./config shared zlib
make && make install
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
echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
ldconfig -v

(2)查看openssl版本

代码语言:javascript
复制
www@TinywanAliYun: openssl version
OpenSSL 1.0.2j  26 Sep 2016

2、重新编译Openresty

(1)本次将下载最新版本:openresty-1.13.6.1.tar.gz

代码语言:javascript
复制
wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar zxvf openresty-1.13.6.1.tar.gz 
cd openresty-1.13.6.1/

(2)修改加载openssl 方式

代码语言:javascript
复制
vim /home/www/build/openresty-1.13.6.1/bundle/nginx-1.13.6/auto/lib/openssl/conf
vim /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/auto/lib/openssl/conf

注意:这里是修改为两个不同目录的openssl的配置文件

代码语言:javascript
复制
# 修改前:
#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_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"

我这里是直接注释掉,添加新的

(3)重新编译Openresty ,添加–with-openssl参数

代码语言:javascript
复制
./configure \
--user=www \
--group=www \
--prefix=/usr/local/openresty \
--with-luajit \
--with-http_v2_module \
--with-http_realip_module \
--with-http_mp4_module \
--with-stream \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-openssl=/usr/local/ssl \
--with-openssl-opt="enable-tlsext" \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_stub_status_module \
--with-http_xslt_module \
--add-dynamic-module=/home/www/build/nginx-ts-module \
--add-dynamic-module=/home/www/build/nginx-rtmp-module \
--add-dynamic-module=/home/www/build/nginx-module-vts \
--add-module=/home/www/build/ngx_cache_purge-2.3 \......make

注意:这里只要编译就可以啦,没必要make install (会覆盖原来已经安装好的内容)

(4)修改Nginx 二进制文件,如果Nginx服务正在运行,必须停止,否则无法覆盖。

首先备份二进制文件一份:

代码语言:javascript
复制
cp /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx.old

覆盖旧二进制文件

代码语言:javascript
复制
cp -f /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/objs/nginx  /usr/local/openresty/nginx/sbin/nginx

如果动态编译了其他模块或者同一个模块的不同版本,则同上一并复制过去

代码语言:javascript
复制
cp -f /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/objs/ngx_rtmp_module.so /usr/local/openresty/nginx/modules/ngx_rtmp_module.so

cp -f /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/objs/ngx_http_ts_module.so /usr/local/openresty/nginx/modules/ngx_http_ts_module.so

cp -f /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/objs/ngx_http_vhost_traffic_status_module.so /usr/local/openresty/nginx/modules/ngx_http_vhost_traffic_status_module.so

(5)重启服务,查看配置信息

代码语言:javascript
复制
sudo systemctl start nginx

配置详情

代码语言:javascript
复制
www@TinywanAliYun:~/build/openresty-1.13.6.1/build/nginx-1.13.6/objs$ sudo /usr/local/openresty/nginx/sbin/nginx -V
nginx version: openresty/1.13.6.1
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) 
built with OpenSSL 1.0.2j  26 Sep 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0
 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --user=www --group=www --with-http_v2_module --with-http_realip_module
 --with-http_mp4_module --with-stream --with-http_ssl_module --with-openssl=/usr/local/ssl
 --with-openssl-opt=enable-tlsext --with-http_stub_status_module --with-http_xslt_module
 --add-dynamic-module=/home/www/build/nginx-ts-module --add-dynamic-module=/home/www/build/nginx-rtmp-module
 --add-dynamic-module=/home/www/build/nginx-module-vts 
 --add-dynamic-module=/home/www/build/ngx_cache_purge-2.3
 --with-stream --with-stream_ssl_module

 --user=www --group=www --with-http_v2_module 
 --with-http_ssl_module --with-openssl=/usr/local/ssl --with-openssl-opt=enable-tlsext --with-http_stub_status_module --with-http_xslt_module 
--add-dynamic-module=/home/www/build/nginx-ts-module 
--add-dynamic-module=/home/www/build/nginx-rtmp-module 
--add-dynamic-module=/home/www/build/nginx-module-vts 
--add-dynamic-module=/home/www/build/ngx_cache_purge-2.3 
--with-stream --with-stream_ssl_module

三、配置Nginx.conf

代码语言:javascript
复制
server {
        listen       443 ssl http2;
        server_name  www.tinywan.com;
        set $root_path /home/www;
        root $root_path;

        ssl on;
        ssl_certificate      /etc/letsencrypt/live/www.tinywan.com/fullchain.pem;
        ssl_certificate_key  /etc/letsencrypt/live/www.tinywan.com//privkey.pem;
        server_tokens off;


        location / {
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
        }
}

四、测试

1、测试工具

Chrome 的 Net-internals 工具 (Chrome 中通过 chrome://net-internals/#http2 可以访问)

2、测试结果

注意:浏览器Header 请求头变化

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源技术小栈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HTTP/2.0 是什么
  • 一、依赖
  • 二、编译安装
    • 1、openssl 编译安装
      • 2、重新编译Openresty
      • 三、配置Nginx.conf
      • 四、测试
        • 1、测试工具
          • 2、测试结果
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档