如何在ubuntu 20 和nginx 上实现加密保护 https

最近,我的网址被黑了,网友说你怎么不用https,还用http 。 他说用了https ,他的网址就安全了。我就查找了一下 cerbot let's encrypt。找到下面原文,在我主机上实现了 http 到 https 的转变,让我网址更安全一点。这个环境是ubunt20 安装了nginx 。本文假定配置的域名是 example.com,实际情况当然要用你自己的域名。

没有或不怎么懂nginx,可以看 nginx配置实战入门

学习的原文内容是:How To Secure Nginx with Let's Encrypt on Ubuntu 20.04

介绍

Let’s Encrypt是一个证书颁发机构(CA),它提供了一种获取和安装免费TLS / SSL证书的简便方法,从而可以在网络服务器上启用加密的HTTPS。 它通过提供一个软件客户端Certbot来简化该过程,该客户端尝试使大多数(如果不是全部)所需步骤自动化。 当前,在Apache和Nginx上,获取和安装证书的整个过程都是完全自动化的。

在本教程中,您将使用Certbot在Ubuntu 20.04上获取Nginx的免费SSL证书,并将证书设置为自动更新。

本教程将使用单独的Nginx服务器配置文件而不是default 文件。 建议为每个域名创建一个Nginx服务器块文件,因为它有助于避免常见错误,并将default 文件保留为后备配置。

安装 Certbot

安装cerbot 和他的nginx 扩展

sudo apt install certbot python3-certbot-nginx

Certbot现在可以使用了,但是为了使其能够为Nginx自动配置SSL,我们需要验证一些Nginx的配置。

验证Nginx的配置

nginx配置实战入门中就配置了这个服务器块文件,名字是example.com ,路径是 /etc/nginx/sites-available 

sudo nano /etc/nginx/sites-available/example.com 查看这个文件:
 

server {
        listen 80;
        listen [::]:80;
 
        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;
 
        server_name example.com www.example.com;
 
        location / {
                try_files $uri $uri/ =404;
        }
}

这里并不关心整个配置文件,关心的只是 server_name 行。

...
server_name example.com www.example.com;
...

如果是这样就不用管了,不是的话,改成这样。

测试配置用:

sudo nginx -t
重载使配置修改生效是:

sudo systemctl reload nginx

允许 HTTPS 通过防火墙

查看当前防火墙状态:

sudo ufw status
现在应该是允许http 过,但没有https

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTP                 ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

执行下面命令,添加https, 去掉多余的http (full 包含了)。

sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'
再检查状态(sudo ufw status)应该是:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

获取SSL证书

Certbot提供了多种插件获取SSL证书的方法。 Nginx插件将负责重新配置Nginx并在必要时重新加载配置。 要使用此插件,请键入以下内容:

sudo certbot --nginx -d example.com -d www.example.com

这里用--nginx插件运行certbot,并使用-d指定我们希望证书对其有效的域名。

如果这是第一次运行certbot,系统将提示输入电子邮件地址并同意服务条款。 完成此操作后,certbot将与Let's Encrypt服务器通信,然后进行质询以验证您是否控制了要为其申请证书的域。

如果成功,certbot将询问如何配置HTTPS设置。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

选择1,2,或者c,我是选择2,然后按Enter。 配置将被更新,并且Nginx将重新加载以获取新设置。 certbot将以一条消息结束,告诉该过程已成功完成,并且告诉证书的存储位置:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2020-08-18. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

证书已下载,安装和加载。 尝试使用https://重新加载你的网站,并注意浏览器的安全指示器。 它应表明该站点已正确保护,通常带有一个锁定图标。 如果使用SSL Labs服务器测试来测试服务器,它将获得A级。

验证Certbot Auto-Renewal

加密证书仅有效九十天。 这是为了鼓励用户自动化其证书续订过程。 安装的certbot程序包通过添加systemd计时器来解决这一问题,该计时器每天运行两次,并在到期后三十天内自动续订任何证书。

可以使用systemctl查询计时器的状态:

sudo systemctl status certbot.timer

测试续订过程,可以使用certbot进行试运行:

sudo certbot renew --dry-run

如果没有看到任何错误,则说明一切就绪。 必要时,Certbot将续订证书并重新加载Nginx以获取更改。 如果自动续订过程失败,Let’s Encrypt将向指定的电子邮件发送一条消息,在证书即将过期时警告你。

介绍完毕

猜你喜欢

转载自blog.csdn.net/leon_zeng0/article/details/113767458