文档主要是用于说明在本机环境如何配置 https服务
域名是阿里云上购买的真实域名,只有真实域名才能申请 ssl证书
1、首先购买域名
这里在阿里云购买了域名 xiaotu.icu
2、购买 SSL 证书
获得两个用于nginx的证书文件:4290328_xiaotu.icu.pem、4290328_xiaotu.icu.key
3、修改本机 Host文件
由于是用于本机测试,不能使用网络上的DNS 服务器,只能修改本机hosts
sudo vim /etc/hosts
Hosts文件中配置域名的本机DNS
127.0.0.1 xiaotu.icu
采用 ping 命令确认 本机 DNS 修改成功
dai@dai-laptop:~$ ping xiaotu.icu
PING xiaotu.icu (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.034 ms
4、安装nginx
本次系统为ubuntu 20
sudo apt-get install nginx
测试nginx是否安装成功
浏览器打开 http://127.0.0.1/ ,
当然也可以采用域名访问 http://xiaotu.icu
5、为 nginx 配置 ssl 证书
将之前购买的 ssl 证书 文件保存到如下目录: /home/dai/files
sudo vim /etc/nginx/sites-enabled/default
初始配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
在 server 段中增加如下配置
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /home/dai/files/4290328_xiaotu.icu.pem;
ssl_certificate_key /home/dai/files/4290328_xiaotu.icu.key;
6、重启 nginx 服务
service nginx restart