侧边栏壁纸
博主头像
Amelie

但行好事,莫问前程

  • 累计撰写 19 篇文章
  • 累计创建 34 个标签
  • 累计收到 0 条评论

hexo开启HTTPS(SSL证书)

Amelie
2020-06-15 / 0 评论 / 0 点赞 / 36 阅读 / 2,325 字
温馨提示:
本文最后更新于 2022-03-11,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

环境:阿里云Centos 7.3

首先安装 Nginx ,具体方法自行搜索(小声bb:本站就有)。

获得证书文件,方法请自行翻阅本站查找。

这里以 letsencrypt 证书为例。

letsencrypt 证书文件位置

/etc/letsencrypt/live/<域名>/cert.pem
/etc/letsencrypt/live/<域名>/chain.pem
/etc/letsencrypt/live/<域名>/fullchain.pem
/etc/letsencrypt/live/<域名>/privkey.pem

只有 fullchain.pemprivkey.pem 文件对我们有用。

配置 Nginx 在 /etc/nginx/nginx.conf 中的 http 大括号里追加:

server{
    listen 80;
    listen [::]:80;
    server_name <域名>;
    rewrite ^(.*) https://$host$1 permanent;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name <域名>;
    ssl_certificate /etc/letsencrypt/live/<域名>/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/<域名>/privkey.pem;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/conf.d/dhparam.pem; # openssl dhparam -out /etc/nginx/conf.d/dhparam.pem 2048
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 60m;
    ssl_session_tickets off;

    charset utf-8,gbk,gb2312;

    location / {
        root <网站路径>;
        index index.html index.htm index.php;
        try_files $uri $uri/ /index.html?$args /index.php?$args;
    }

    error_page 497 https://$server_name:$server_port$request_uri;
    error_page 404 /404.html;

    # 设置浏览器缓存文件的时长
    #location ~.*\.(jpg|png|jpeg|mp3|flac|mp4)$ {
    #    expires 30d;
    #}
    #location ~.*\.(js|css)?$ {
    #    expires 1h;
    #}

    # 配置下载目录
    location /downloads {
        alias /<你的hexo目录>/public/resource;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        if ($request_filename ~* ^.*?\.(.*)$){
            add_header Content-Disposition 'attachment;';
        }
    }
    
    # gzip
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;

    # security headers
    add_header X-Frame-Options           "SAMEORIGIN" always;
    add_header X-XSS-Protection          "1; mode=block" always;
    add_header X-Content-Type-Options    "nosniff" always;
    add_header Referrer-Policy           "no-referrer-when-downgrade" always;
    add_header Content-Security-Policy   "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;

    # . files
    location ~ /\.(?!well-known) {
        deny all;
    }

}

重启 Nginx

nginx -s reload
0

评论区