个人网站配置HTTPS

让NGINX支持SSL

需要编译时支持ssl,可以sbin/nginx -V 来查看confiture参数,如果当时没有支持,那么需要重新编译安装。 编译参数前面已经有一篇文章了。nginx编译参数,也不用全加,用--with-http_ssl_module 就可以了。

生成证书

主要参考这个letsencrypty,可以生成免费证书。 生成方式也很简单,读上面的文章基本就能明白。ubuntu+nginx.

大致步骤如下:

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto
./path/to/certbot-auto certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.is -d m.thing.is

执行完之后系统中会生成这些文件。
系统中生成的文件
This command will obtain a single cert for example.com, www.example.com, thing.is, and m.thing.is; it will place files below /var/www/example to prove control of the first two domains, and under /var/www/thing for the second pair.

Automating renewal

上面生成的证书,有效期好像是1个月,所以需要到期自己重新renewal一下。方法如下:

./path/to/certbot-auto renew --dry-run
./path/to/certbot-auto renew --quiet --no-self-upgrade

配置NGINX

配置就不多说了.首先需要配置2个server,监听2个端口。这样可以强制将80端口的请求重定向至443端口。https本身监听的是443端口。最主要的是ssl中间那3行。将步骤2中生成的对应key写在nginx的配置文件里。注意改成你的具体路径。

server {

    listen       443;
    server_name  blog.nofile.cc;

    ssl                  on;
    ssl_certificate      /xxxx/letsencrypt/live/yoursite/fullchain.pem;
    ssl_certificate_key  /xxxx/letsencrypt/live/yoursite/privkey.pem;
   
     location / {
        #这个地方指定被访问的文件夹位置
        root   /your/webroot/;
        index  index.html;
    }
}

server {
    listen      80;
    server_name blog.nofile.cc;
    return 301 https://blog.nofile.cc$request_uri;  
}

配置好之后,重启nginx,应该就可以看到绿色的锁了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,951评论 19 139
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,079评论 0 9
  • 前段时间,看见很多大会都在分享全站HTTPS的经验。HTTPS固然好,前提是SSL证书,并且签发证书的机构要靠谱。...
    人世间阅读 6,078评论 0 5
  • 摘要 本文主要是在测试https服务时对搭建https服务器的一些实践总结。TLS协议的介绍部分来源于对RFC52...
    东方胖阅读 8,488评论 0 10
  • 10月22号到11月18号的尾巴,停笔将近一个月的时间,而现在,我又想记下些什么了。 总是有一种错觉,总是会听见时...
    雍雍吶阅读 613评论 0 1