Nginx 学习笔记(二)Web 服务启用 HTTP/2

简介: 个人网站提升为HTTP2.0协议,加速访问一、条件1、openssl的版本必须在1.0.2e及以上2、nginx的版本必须在1.9.5以上二、编译安装1、openssl 编译安装(1)开始wget --no-check-certificate https://www.

个人网站提升为HTTP2.0协议,加速访问

一、条件

1、openssl的版本必须在1.0.2e及以上

2、nginx的版本必须在1.9.5以上

二、编译安装

1、openssl 编译安装

(1)开始

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版本

www@TinywanAliYun: openssl version
OpenSSL 1.0.2j  26 Sep 2016

232

2、重新编译Openresty

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

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 方式

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的配置文件

# 修改前:
#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参数

./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服务正在运行,必须停止,否则无法覆盖

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

cp /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx.old

覆盖旧二进制文件

cp -f /home/www/build/openresty-1.13.6.1/build/nginx-1.13.6/objs/nginx  /usr/local/openresty/nginx/sbin/nginx

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

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)重启服务,查看配置信息

sudo systemctl start nginx

配置详情

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

    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 请求头变化

 

五、参考文章

1、HTTP 2.0 详细介绍

3、HTTP协议简介

4、nginx的http2.0性能太逆天了,HTTPS网站性能优化

5、http2.0与http1.X的区别

6、面试时如何优雅的谈论HTTP/1.0/1.1/2.0

7、HTTP/2.0与HTTP/1.1协议区别

8、nginx配置http2无效不起作用

9、[翻译]Dropbox Web 服务启用 HTTP/2:经验和观察

10、如何启用curl命令HTTP2支持

目录
相关文章
|
22天前
|
数据采集 Web App开发 API
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
本文介绍了如何使用FastAPI和Selenium搭建RESTful接口,访问免版权图片网站Pixabay并采集图片及其描述信息。通过配置代理IP、User-Agent和Cookie,提高爬虫的稳定性和防封禁能力。环境依赖包括FastAPI、Uvicorn和Selenium等库。代码示例展示了完整的实现过程,涵盖代理设置、浏览器模拟及数据提取,并提供了详细的中文注释。适用于需要高效、稳定的Web数据抓取服务的开发者。
71 15
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
|
4月前
|
XML JSON 数据安全/隐私保护
Web服务
【10月更文挑战第18天】Web服务
83 9
|
4月前
|
自然语言处理 大数据 应用服务中间件
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
大数据-172 Elasticsearch 索引操作 与 IK 分词器 自定义停用词 Nginx 服务
105 5
|
17天前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
53 7
|
4月前
|
XML JSON 安全
Web服务是通过标准化的通信协议和数据格式
【10月更文挑战第18天】Web服务是通过标准化的通信协议和数据格式
213 69
|
3月前
|
负载均衡 前端开发 JavaScript
Nginx 代理多服务
以上是 Nginx 代理多服务的几种常见方式,在实际应用中,可以根据具体的业务需求和系统架构选择合适的代理方式,并结合其他 Nginx 的功能和配置来优化和完善系统的性能和功能。
|
3月前
|
Go UED
Go Web服务中如何优雅平滑重启?
在生产环境中,服务升级时如何确保不中断当前请求并应用新代码是一个挑战。本文介绍了如何使用 Go 语言的 `endless` 包实现服务的优雅重启,确保在不停止服务的情况下完成无缝升级。通过示例代码和测试步骤,详细展示了 `endless` 包的工作原理和实际应用。
90 3
|
3月前
|
JSON Go UED
Go Web服务中如何优雅关机?
在构建 Web 服务时,优雅关机是一个关键的技术点,它确保服务关闭时所有正在处理的请求都能顺利完成。本文通过一个简单的 Go 语言示例,展示了如何使用 Gin 框架实现优雅关机。通过捕获系统信号和使用 `http.Server` 的 `Shutdown` 方法,我们可以在服务关闭前等待所有请求处理完毕,从而提升用户体验,避免数据丢失或不一致。
51 1
|
4月前
|
计算机视觉 Python
Flask学习笔记(六):基于Flask的摄像头-web显示代码(可直接使用)
这篇文章是关于如何使用Flask框架结合OpenCV库,通过电脑摄像头实现视频流在网页上的实时显示,并提供了单摄像头和多摄像头的实现方法。
190 2
Flask学习笔记(六):基于Flask的摄像头-web显示代码(可直接使用)
|
3月前
|
XML 安全 PHP
PHP与SOAP Web服务开发:基础与进阶教程
本文介绍了PHP与SOAP Web服务的基础和进阶知识,涵盖SOAP的基本概念、PHP中的SoapServer和SoapClient类的使用方法,以及服务端和客户端的开发示例。此外,还探讨了安全性、性能优化等高级主题,帮助开发者掌握更高效的Web服务开发技巧。

热门文章

最新文章