为网站配置SSL

HTTPS (全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性。HTTPS 在HTTP 的基础下加入SSL 层,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL。 HTTPS 存在不同于 HTTP 的默认端口及一个加密/身份验证层(在 HTTP与 TCP 之间),这个系统提供了身份验证与加密通讯方法。

现状


证书申请

除了向公有云申请证书, 也可使用 自签名openssl生成的证书,但方便起见还是使用云厂商提供的证书.

一般免费版,只有一年有效期.到期需要重新申请&更换


Nginx配置

将证书文件上传至/usr/local/openresty/nginx/conf/cert目录下.

博客项目当前的conf配置如下:

复制代码
server {
    listen      80;
    server_name dashen.tech www.dashen.tech;
    access_log  /var/log/blog.access.log main;
    error_log  /var/log/blog.error.log;

  location / {
        root        /home/ubuntu/cuishuang.github.io;
        index       index.html;
        expires     1d;
        add_header  Cache-Control public;
        access_log  off;
    }
}

新增启用https的配置:

复制代码
server {
      listen        443 ssl;                                                 
      server_name    dashen.tech www.dashen.tech;  #域名                         
      ssl_certificate      /usr/local/openresty/nginx/conf/cert/shuang_blog.pem;  #证书路径     
      ssl_certificate_key  /usr/local/openresty/nginx/conf/cert/shuang_blog.key;  #key路径             
      ssl_session_cache    shared:SSL:1m;   #s储存SSL会话的缓存类型和大小                       
      ssl_session_timeout  5m; #会话过期时间 

      access_log  /var/log/blog.access.log main;
      error_log  /var/log/blog.error.log;

location / {
        root        /home/ubuntu/cuishuang.github.io;
        index       index.html;
        expires     1d;
        add_header  Cache-Control public;
        access_log  off;
    }                                                     
  }

删掉之前的conf. 重启nginx,访问https://www.dashen.tech^[1]^,已能正常访问.

再访问之前的网址https://dashen.tech^[2]^,则

配置将http访问自动跳转到https

再增加一段配置:

复制代码
server {
    listen      80;
    server_name dashen.tech www.dashen.tech;
    access_log  /var/log/blog.access.log main;
    error_log  /var/log/blog.error.log;

    return      301 https://$server_name$request_uri; #这是nginx最新支持的写法

  location / {
        root        /home/ubuntu/cuishuang.github.io;
        index       index.html;
        expires     1d;
        add_header  Cache-Control public;
        access_log  off;
    }
}

参考: Nginx强制跳转Https[3]

再次重启nginx,这时请求https://dashen.tech^[4]就可以跳转到https://www.dashen.tech[5]^


但因为网站下有部分资源使用了http,所以浏览器依然没有变为安全锁,

可参考Hexo启用https加密连接[6],

也可右键查看哪些请求使用了http,将其修改为https即可~

参考资料

1

https://www.dashen.tech: https://www.dashen.tech

2

https://dashen.tech: https://dashen.tech

3

Nginx强制跳转Https: https://www.jianshu.com/p/116fc2d08165

4

https://dashen.tech: https://dashen.tech

5

https://www.dashen.tech: https://www.dashen.tech

6

Hexo启用https加密连接: https://note.youdao.com/web/#/file/recent/note/WEBe69d252eb353dd5ee0210d053ec0cc3a/

本文由mdnice多平台发布

相关推荐
熊小猿27 分钟前
在 Spring Boot 项目中使用分页插件的两种常见方式
java·spring boot·后端
paopaokaka_luck31 分钟前
基于SpringBoot+Vue的助农扶贫平台(AI问答、WebSocket实时聊天、快递物流API、协同过滤算法、Echarts图形化分析、分享链接到微博)
java·vue.js·spring boot·后端·websocket·spring
小蒜学长2 小时前
springboot酒店客房管理系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
橙子家2 小时前
Serilog 日志库简单实践(一):文件系统 Sinks(.net8)
后端
Yeats_Liao3 小时前
Go Web 编程快速入门 13 - 部署与运维:Docker容器化、Kubernetes编排与CI/CD
运维·前端·后端·golang
Yeats_Liao3 小时前
Go Web 编程快速入门 14 - 性能优化与最佳实践:Go应用性能分析、内存管理、并发编程最佳实践
前端·后端·性能优化·golang
七夜zippoe4 小时前
仓颉语言核心特性深度解析——现代编程范式的集大成者
开发语言·后端·鸿蒙·鸿蒙系统·仓颉
软件架构师-叶秋4 小时前
spring boot入门篇之开发环境搭建
java·spring boot·后端
QX_hao5 小时前
【Go】--接口(interface)
开发语言·后端·golang
superman超哥5 小时前
仓颉语言中正则表达式引擎的深度剖析与实践
开发语言·后端·仓颉