第1章 概述
我们前面介绍了用Nginx搭建一个下载站,但是默认是http访问,对于一些重要的数据文件的传输会有一定的不安全性。
小编这篇文章将会把访问的地址调整https添加证书。
第2章 环境准备
演示环境我们使用了一台RockyLinux 9.5服务器进行部署。
访问逻辑如下,管理员访问服务器上传文件,用户访问web下载文件,访问地址配置为https。
第3章 环境部署
相关部署文件镜像站的说明见《Nginx轻松部署下载站》,本文主要介绍配置https。
1、使用ssh远程工具,远程到服务器。
2、检查确认是否有openssl工具。
[root@localhost ~]# openssl -v
3、使用openssl工具签发一张自签名证书。
(1)生成key文件
[root@localhost ~]# openssl genrsa -out 192_168_96_31.key 2048
(2)生成scr请求文件
[root@localhost ~]# openssl req -new -key 192_168_96_31.key -out 192_168_96_31.csr
(3)生成证书文件
[root@localhost ~]# openssl x509 -req -days 36500 -in 192_168_96_31.csr -signkey 192_168_96_31.key -out 192_168_96_31.crt
4、将生成的证书文件和key文件复制到/etc/nginx/ssl,并命名为server.crt、server.key。
[root@localhost ~]# mkdir /etc/nginx/ssl
[root@localhost ~]# cp 192_168_96_31.crt /etc/nginx/ssl/server.crt
[root@localhost ~]# cp 192_168_96_31.key /etc/nginx/ssl/server.key
5、修改nginx配置文件。
[root@localhost ~]# vim /etc/nginx/conf.d/server.conf
配置文件内容如下:
server {
listen 443 ssl; ##指定服务端口
server_name localhost;
root /opt/data; ##指定根目录
location / {
autoindex on; ##自动生成主目录
autoindex_localtime on; ##使用本地时间
}
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
}
6、使用“nginx -t”检验配置是否正确。
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
7、重启nginx 服务。
[root@localhost ~]# systemctl restart nginx
7、添加防火墙端口例外。
[root@localhost ~]# firewall-cmd --permanent --add-port=443/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
8、访问网页测试,已经添加证书,点击继续访问。
第4章 总结
希望通过此篇文章对于使用Nginx部署配置https访问的介绍可以帮助到有相关需求的朋友。小编在这里愿在为梦想努力拼搏奋斗的工程师们“轻轻松松的工作,快快乐乐的生活”。