安装SSL模块
- 查看是否安装了http_ssl_module模块
/usr/local/nginx/sbin/nginx -V
如果输出结果出现 configure arguments: –with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 配置ssl证书)。
- 下载Nginx安装的源码包
cd /usr/local/src
wget http://nginx.org/download/nginx-1.22.0.tar.gz # 1.22.0 替换成自己版本即可(没有实验不同版本是否能通用)
- 解压安装包
tar -zxvf nginx-1.22.0.tar.gz
- 配置ssl模块
cd nginx-1.22.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 然后使用make命令编译,此时当前目录的objs文件夹中会有一个名字叫做nginx的执行文件,用新的nginx覆盖老的
cp ./objs/nginx /usr/local/nginx/sbin/
- 再次查看是否安装了ssl模块
/usr/local/nginx/sbin/nginx -V
安装成功会出现以下
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
配置ssl证书(在你有证书的前提下)
cd /usr/local/nginx
vim nginx.conf
# 修改nginx的配置如下
server {
listen 443 ssl;
server_name 服务器域名;
ssl_certificate xx.pem; # pem文件的路径
ssl_certificate_key xxx.key; # key文件的路径
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_buffer_size 8k;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 服务器域名;
# http强制到https
return 301 https://$server_name$request_uri;
}
重启nginx
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx