最近因为迁移公司vm上的代码,遇到一些问题。有一台vm配置了https协议,原以为迁移安全证书以后,配置nginx就可以了,但是修改nginx配置文件以后,执行 nginx -t 命令后,报如下错误:
nginx: [warn] invalid parameter "spdy": ngx_http_spdy_module was superseded by ngx_http_v2_module in /usr/local/nginx/conf/sites-enable
AI 代码解读
在网上google了一下,发现原因和nginx的版本有关。迁移前的vm环境,nginx是1.8版本的,新vm上的nginx是1.10的。按照官方的说明,在1.9.5以上版本,如果要启用ssl协议,需要将原来配置ssl的写法改一下:
旧的ssl配置写法:listen 443 ssl spdy;
新的写法:listen 443 default_server ssl http2
修改后执行 nginx -t,报错如下信息:
nginx: [emerg] the "http2" parameter requires ngx_http_v2_module in /usr/local/nginx/conf/sites-enable
AI 代码解读
原因是nginx从1.9.5开始,已经用 http_v2_module 模块替换了 ngx_http_spdy_module ,并正式开始支持http2协议。所以,没办法了,只好下载新版nginx的源码,重新编译升级。
关于SPDY和HTTP2协议的一些内容说明,可以一下内容:
(1)SPDY的定义
(2)HTTP2协议的定义
下面记录一下这次升级nginx和配置ssl的过程。
一.注意事项:
1.要开启HTTP/2协议支持,需要在nginx 1.10以上版本并且需要openssl库的版本在1.0.2以上编译。
2.http2.0只支持开启了https的网站。
二. 升级OpenSSL
在http2.0协议中,涉及到ALPN(Application Layer Protocol Negotiation,应用层协议协商)的支持,目前所有主流的Unix服务器系统中内置的OpenSSL库都低于1.0.2版本。通过使用OpenSSL的命令行工具,可以检查当前的http2服务是否支持ALPN。
openssl s_client -alpn h2 -servername topics.orthonline.com.cn -connect topics.orthonline.com.cn:443 < /dev/null | grep 'ALPN'
AI 代码解读
如果报错:
说明当前OpenSSL版本不支持,执行 如下命令,检查OpenSSL的版本:unknown option -alpn
AI 代码解读
我本地当前版本是 OpenSSL 1.0.1e-fips,需要升级。openssl version
AI 代码解读
具体的操作步骤:
1.下载最新版的OpenSSL库编译安装
wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz
tar xzf openssl.tar.gz
cd openssl-1.1.0f
./config --prefix=/usr/local/openssl
make && make install
AI 代码解读
2.替换旧版本库
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
#链接新库文件
ln -s /usr/local/openssl/lib/libssl.so /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/lib/libcrypto.so /usr/local/lib64/libcrypto.so
#检查更新后的openssl依赖库是否是1.1.0f
strings /usr/local/lib64/libssl.so | grep OpenSSL
#显示结果表明已升级到最新版本链接库
OpenSSL 1.1.0f 25 May 2017
#配置openssl库文件的搜索路径
echo '/usr/local/openssl/lib' >> /etc/ld.so.conf
#使修改后的搜索路径生效
ldconfig -v
#查看openssl版本,结果显示升级成功
openssl version
OpenSSL 1.1.0f 25 May 2017
AI 代码解读
三.升级nginx
1.下载最新版nginx源码并解压编译
wget http://nginx.org/download/nginx-1.10.3.tar.gz tar zxvf nginx-1.10.3.tar.gz cd nginx-1.10.3 #编译nginx,添加http_v2模块应用,新版编译命令如下 ./configure --prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --sbin-path=/usr/local/nginx/sbin/nginx \ --pid-path=/usr/local/nginx/nginx.pid \ --error-log-path=/var/log/nginx/nginx-error.log \ --http-log-path=/var/log/nginx/nginx-access.log \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_secure_link_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-openssl=/home/softwares/openssl-1.1.0f
AI 代码解读
编译完成后,执行make,但不执行make install
将旧版本的nginx二进制文件,重命名一个名字,在这期间,当前运行的nginx进程不会停止,不影响应用运行。make
AI 代码解读
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
AI 代码解读
然后将上一步通过make编译好的新版nginx二进制文件,拷贝到运行目录
cp objs/nginx /usr/local/nginx/sbin/nginx
AI 代码解读
在源码目录根目录下,执行更新安装命令
注意:如果原来的相关配置文件中,写有和ssl有关的配置信息,需要先暂时注释掉,否则更新时会报错。make upgrade
AI 代码解读
更新完成后,执行
可以看到nginx已经更新到1.10.3版本。nginx -V
AI 代码解读
完成以上操作,就完成了nginx的更新,并已开启对http2和https的最新支持。
四.修改相关nginx配置文件
在需要打开https协议支持的应用配置文件中,加入如下内容:
重启nginx完成https设置。listen 443 ssl http2;
AI 代码解读