阅读 162

升级nginx以支持http2的方法

本篇文章主要介绍了升级nginx以支持http2的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

一键升级脚本

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

HTTP/2 的优势

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

  1. HTTP/2 采用二进制格式传输数据,而非 HTTP/1.x 的文本格式。二进制格式在协议的解析和优化扩展上带来更多的优势和可能。

  2. HTTP/2 对消息头采用 HPACK 进行压缩传输,能够节省消息头占用的网络的流量。而 HTTP/1.x 每次请求,都会携带大量冗余头信息,浪费了很多带宽资源。头压缩能够很好的解决该问题。

  3. 多路复用,直白的说就是所有的请求都是通过一个 TCP 连接并发完成。HTTP/1.x 虽然通过 pipeline 也能并发请求,但是多个请求之间的响应会被阻塞的,所以 pipeline  至今也没有被普及应用,而 HTTP/2 做到了真正的并发请求。同时,流还支持优先级和流量控制。

  4. Server Push:服务端能够更快的把资源推送给客户端。例如服务端可以主动把 JS 和 CSS 文件推送给客户端,而不需要客户端解析 HTML 再发送这些请求。当客户端需要的时候,它已经在客户端了。

升级HTTP2必要条件

  1. 1.10.0版本以上的nginx基于1.0.2以上版本的openssl编译

  2. 必须支持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

以上就是本文的全部内容,希望对大家的学习有所帮助



文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐