【技术博客】nginx服务器的https协议实现

在本学期软件工程的Alpha和Beta阶段,咱们的服务器部署都是使用基础的http协议,http在网络路由间的信息转发都为明文,这对咱们网站的帐户密码登陆来讲很不安全,所以在Gamma阶段咱们实现了https协议传输。html

1.https证书申请

https简单地说就是http加了一层ssl加密层,加密证书在任何一台计算机上均可以生成,可是由任意第三方生成的证书毫无疑问是不可靠的。所以有了CA这样的证书管理机构,CA在网络通讯的过程当中能够被认为是彻底可信的第三方,使用CA的ssl证书才能够取得客户端浏览器的信任。nginx

首先安装cerbot(在ubuntu上使用apt安装便可),使用下面的命令能够执行cerbot认证:web

certbot certonly

认证过程当中会出现选择:shell

1: Spin up a temporary webserver (standalone)
2: Place files in webroot directory (webroot)

在cerbot中有上面两种认证方式,第一种须要中止服务器,cerbot将创建一个临时服务器进行验证域名对应的IP是否为本机,第二种则不须要中止服务器的服务,须要在指定位置建立文件来验证你对该IP计算机的控制权。咱们使用了第一种验证方式,由于不须要修改nginx配置文件因此更简单。选择验证方式后根据提示一步步填写邮箱、赞成协议便可。ubuntu

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/you.domain.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/you.domain.com/privkey.pem
   Your cert will expire on 2018-01-26. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. 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证书已经申请成功,密钥的文件地址也在其中。可使用下面的命令查看这台计算机上的证书:浏览器

certbot certificates

2.https证书配置

https证书须要加入nginx的配置种,一方面咱们须要在nginx中指定私钥和公钥的文件地址,另外一方面须要大幅修改端口设置,http下的nginx配置能够参考上一篇技术博客。须要注意的是http是约定使用80端口通讯,而https约定使用443端口通讯,因此咱们须要将80端口的流量重定向至https的443端口,确保全部的流量都以https方式传输。安全

server {
    charset utf-8;
    listen 80;
    server_name www.you.domain.com you.domain.com;
    return 301 https://you.domain.com$request_uri;
}

server {
    listen 443 default ssl;
    server_name  blogof33.com;

    ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/you.domain.com/privkey.pem;
    error_log /var/log/nginx/error.log;

    location / {
        alias xxxxx;
    }
}

随后重启nginx服务,至此配置完成。bash

相关文章
相关标签/搜索