Nginx配置SSL证书
SSL证书是一种数字证书,用于验证网站的身份并加密网站与用户浏览器之间传输的数据。它确保传输的数据保持安全,不能被未经授权的人拦截。

SSL证书与HTTPS密切相关。HTTPS是一种安全的通信协议,通过使用SSL证书对网站进行加密,确保数据在传输过程中的安全性。而在上篇文章中,我们通过Nginx搭建静态网站并对网站绑定了域名后,我们通过域名访问静态网站时通过不安全的HTTP通信协议进行数据传输的。
因此,要想保证网站数据传输的安全性,需要尽可能的使用HTTPS通信协议进行数据传输,而要做到这一点,就需要通过Nginx完成SSL证书的配置。
当用户访问一个启用了HTTPS的网站时,浏览器会向服务器请求并接收SSL证书。浏览器会验证证书的有效性,确保它是由受信任的证书颁发机构签发的,并且与网站的域名匹配。
一旦证书验证通过,浏览器和服务器之间的通信将建立安全的加密连接。这样,用户在与网站进行交互、提交敏感信息时,数据将被加密,保护用户的隐私和安全。
一、SSL证书申请
SSL证书的获取渠道很多,既可以花钱购买,也可以免费申请。这里介绍freessl免费申请证书。
1. 进入网站,该注册账号的注册,然后登录,返回首页。
2. 输入需要申请证书的个人域名:nginx.nnlsde.xyz
,然后点击'创建免费的SSL证书',进入ACME配置步骤。

3. 下一步后,进入DCV配置

DCV配置是用于证明你对域名具有完整控制权,因此需要到域名提供商那边提供的域名管理进行配置。我的域名是在namecheap平台购买的。
4. 选择添加一条记录,然后按照给出的主机记录、记录类型、记录值等进行配置,配置完成后点击检测即可进入下一步。
配置完成后可能需要等几分钟才能生效。


5. 检测通过后,就需要在服务器上安装ACME脚本:
sh
curl https://get.acme.sh | sh -s email=你的邮箱

6. 安装ACME脚本后,回到freessl,运行给出的ACME部署指令
acme.sh脚本放在了.acme.sh文件夹下


如果没有把acme.sh脚本配置为全局变量的话,需要进入到放脚本的文件夹中,使用
./acme.sh
来执行脚本。
7. 部署完成后,就得到了证书信息的存放位置。

8. 我们将cert和cert key两个文件单独拿出来,并放在/etc/nginx/
下
linux
cp .acme.sh/nginx.nnlsde.xyz_ecc/nginx.nnlsde.xyz.cer /etc/nginx/
cp .acme.sh/nginx.nnlsde.xyz_ecc/nginx.nnlsde.xyz.key /etc/nginx/

这样,我们就完整的申请到了证书了。
二、Nginx配置
我们申请到证书后,利用Nginx配置是非常简单的。HTTPS协议默认是443端口,因此我们需要设置一个服务器节点,用于监听443端口。
nginx
http {
include mime.types;
server {
listen 443 ssl;
server_name nginx.nnlsde.xyz;
ssl_certificate nginx.nnlsde.xyz.cer;
ssl_certificate_key nginx.nnlsde.xyz.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html/site;
index index.html;
}
}
# 配置HTTP重定向到HTTPS
server {
listen 80;
server_name nginx.nnlsde.xyz;
rewrite ^/(.*)$ https://nginx.nnlsde.xyz:443/$1 permanent;
}
}
events {}
注意:要想远程访问HTTPS,需要把443端口开放,开放操作参考上一篇文章。
