说在前面:
centos7上配置https之前需要先配置 ngx_http_ssl_module,否则即使在nginx.conf中配置了https,但在启动nginx的时候会报错:
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:118
如下:
[root@nginx sbin]# ./nginx
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:118
正式开始:
一 nginx 开启SSL模块
1.1 nginx如果未开启SSL模块,配置https时提示错误
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:118
原因也很简单,nginx缺少http_ssl_module模块,编译安装的时候带上 --with-http_ssl_module 配置就行了,但往往实际的情况是我们的nginx已经安装过了,在这种情况要怎么添加模块,其实很简单,往下看:做个说明:我的nginx安装目录是/usr/local/nginx这个目录,我的源码包在/usr/local/nginx-1.14.0 (有的人喜欢把源码包放在/usr/local/src/nginx-1.14.0)
1.2 切换到源码包
cd /usr/local/nginx-1.14.0
查看nginx原有的模块
/usr/local/nginx/sbin/nginx -V
[root@nginx local]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@nginx local]#
在configure arguments:后面显示的原有的configure 参数如下:
--prefix=/usr/local/nginx
那么我们的新配置信息就应该这样写:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
进入源码包位置/usr/local/nginx-1.14.0 运行上面的命令即可,等配置完。
即执行配置命令:
配置完成后,运行命令
make
这里不要进行make install, 否则就是覆盖安装
然后备份原有已安装好的nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
检查nginx是否是运行状态,通过命令 ps -ef|grep nginx , 提示:运行中的nginx会有master进程 和 worker进程
如果是运行状态,进入/usr/local/nginx/sbin/目录下,停掉正在运行的nginx,通过命令 /usr/local/nginx/sbin/nginx -s stop
然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要处于停止运行状态)
进入源码包位置/usr/local/nginx-1.14.0, 将刚刚编译好的nginx文件覆盖掉原有的nginx文件
cp ./objs/nginx /usr/local/nginx/sbin/
[root@nginx nginx-1.14.0]# cp ./objs/nginx /usr/local/nginx/sbin/
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y
[root@nginx nginx-1.14.0]#
然后使用命令查看是否已经加入成功
/usr/local/nginx/sbin/nginx -V
[root@nginx nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@nginx nginx-1.14.0]#
二 nginx配置文件中配置https加密证书
题外话:https 加密证书申请是否需要绑定域名?
https证书申请肯定要提供域名,配置的时候也需要绑定域名的,如果没有域名也可以绑定IP,两者必须要有一个才能申请。
2.1 生成服务器的RSA私钥
[root@nginx ~]# mkdir /usr/local/nginx/conf/ssl
[root@nginx ~]# cd /usr/local/nginx/conf/ssl
[root@nginx ssl]# openssl genrsa -out server.key 2048
如图:
2.2 生成服务器的CSR证书请求文件
CSR证书请求文件是服务器的公钥,用于提交给CA机构进行签名。生成csr(CSR,Cerificate Signing Request,CSR是您的公钥证书原始文件,包含了您的服务器信息和您的单位信息,需要提交给CA认证中心。)的命令如下:
[root@nginx ssl]# openssl req -new -key server.key -out server.csr
在上述命令中,req 表示证书签发申请, -new 表示新请求,-key server. key 指定私钥为 server. key,-out server. csr 表示生成的 CSR 证书请求文件的名称为 server. csr。
值得一提的是,在填写 common name 信息时,必须与实际使用 HTTPS 的网站域名吻合,否则会引发浏览器警报。
2.3 CA为服务器认证证书
[root@nginx ssl]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
如图:
上述指令用于使用CA的私钥server.key 为服务器CSR证书申请文件server.csr 进行签名认证。其中,x509是自签名证书格式,days 365 用于设置签发证书的有效期为365天。
2.4 在nginx.conf中配置https加密证书
server {
listen 443 ssl;
server_name 192.168.0.244;
ssl_certificate /usr/local/nginx/conf/ssl/server.crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}