小知识:升级nginx以支持http2的方法

本文介绍了升级nginx以支持http2的方法,分享给大家,具体如下:

一键升级脚本

https://github.com/whisshe/upHttp2.git

HTTP/2 的优势

相比 HTTP/1.x,HTTP/2 在底层传输做了很大的改动和优化:

HTTP/2 采用二进制格式传输数据,而非 HTTP/1.x 的文本格式。二进制格式在协议的解析和优化扩展上带来更多的优势和可能。 HTTP/2 对消息头采用 HPACK 进行压缩传输,能够节省消息头占用的网络的流量。而 HTTP/1.x 每次请求,都会携带大量冗余头信息,浪费了很多带宽资源。头压缩能够很好的解决该问题。 多路复用,直白的说就是所有的请求都是通过一个 TCP 连接并发完成。HTTP/1.x 虽然通过 pipeline 也能并发请求,但是多个请求之间的响应会被阻塞的,所以 pipeline  至今也没有被普及应用,而 HTTP/2 做到了真正的并发请求。同时,流还支持优先级和流量控制。 Server Push:服务端能够更快的把资源推送给客户端。例如服务端可以主动把 JS 和 CSS 文件推送给客户端,而不需要客户端解析 HTML 再发送这些请求。当客户端需要的时候,它已经在客户端了。

升级HTTP2必要条件

1.10.0版本以上的nginx基于1.0.2以上版本的openssl编译 必须支持https

软件安装位置

openssl

1、编译位置

/usr/local/openssl

2、软链接位置

/usr/bin/openssl

/usr/include/openssl

nginx

1、编译及配置文件位置

/etc/nginx

升级所有游戏服务器的nginx和OpenSSL

升级openssl

1、将旧版本的openssl备份

?
1
2
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old

2、下载1.1版本的openssl,并解压编译

?
1
2
3
4
5
6
7
8
9
10
11
tar zxvf openssl-1.1.0g.tar.gz
cd openssl-1.1.0g/
./config –prefix=/usr/local/openssl shared zlib
make depend
make && make install
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl(首选)
cd /usr/local/openssl/lib
ln -s libssl.so.1.1 libcrypto.so.1.1 /lib/x86_64-linux-gnu/
echo /usr/local/openssl/lib >> /etc/ld.so.conf

3、验证openssl版本信息

?
1
2
3
4
openssl version
if [[ `openssl version |awk {print $2}` == 1.1.0g ]];then
echo ok
fi

nginx升级(源码编译)

原配置文件不会丢失

1、下载源码包

?
1
wget http://nginx.org/download/nginx-1.12.2.tar.gz

2、解压编译安装

编译安装 nginx 时,–with-openssl 参数默认只支持OpenSSL的源代码,不支持已编译好的 OpenSSL。可以在nginx的解压目录下修改auto/lib/openssl/conf

sed “s/.openssl\///” c

将文件中的.openssl去掉,就可以支持编译之后的openssl路径了

?
1
2
3
4
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”

正式编译

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
./configure –prefix=/etc/nginx \
–sbin-path=/usr/sbin/nginx \
–conf-path=/etc/nginx/nginx.conf \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx.pid \
–lock-path=/var/run/nginx.lock \
–http-client-body-temp-path=/var/cache/nginx/client_temp \
–http-proxy-temp-path=/var/cache/nginx/proxy_temp \
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
–http-scgi-temp-path=/var/cache/nginx/scgi_temp \
–with-http_ssl_module \
–with-http_realip_module \
–with-http_addition_module \
–with-http_sub_module \
–with-http_dav_module \
–with-http_flv_module \
–with-http_mp4_module \
–with-http_gunzip_module \
–with-http_gzip_static_module \
–with-http_random_index_module \
–with-http_secure_link_module \
–with-http_stub_status_module \
–with-http_auth_request_module \
–with-threads \
–with-stream \
–with-stream_ssl_module \
–with-http_slice_module \
–with-mail \
–with-mail_ssl_module \
–with-file-aio \
–with-http_v2_module \
–with-openssl=/usr/local/openssl

修改nginx配置

?
1
listen 443 ssl http2;

出现的问题及解决

centos7下openssl version报错openssl: error while loading shared libraries: libssl.so.1.1

?
1
2
echo /usr/local/openssl/lib >> /etc/ld.so.conf # 将openssl编译路径的lib库加入到/etc/ld.so.conf
ldconfig -v|grep libssl  # 检查是否添加成功

nginx重启出现错误,nginx: [emerg] mkdir() “/var/cache/nginx/client_temp” failed

?
1
2
3
ls -l /var/cache/nginx/client_temp#检查该文件夹是否存在和文件夹的权限
mkdir -p /var/cache/nginx/client_temp #文件夹不存在则生成文件夹
chown -R www-data:www-data /var/cache/nginx/client_temp # 将文件夹所有者和组改为nginx的运行用户,具体见nginx.conf

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/60e84fc98f93

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:使用Linux要改掉几个的命令习惯

2023-4-19 3:02:21

建站知识

小知识:Mac OS X中设置VIM语法高亮的方法

2023-4-19 3:10:37

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索