本文介绍了升级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备份

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

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

 wget https://www.openssl.org/source/openssl-1.1.0g.tar.gz
 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版本信息

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

nginx升级(源码编译)

原配置文件不会丢失

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路径了

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"

正式编译

 ./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配置

listen 443 ssl http2;

出现的问题及解决

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

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

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

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

点赞(182)

评论列表共有 0 条评论

立即
投稿
返回
顶部