背景

​ 基于上一篇 Linux(Centos)安装Hexo 后,默认是http访问,但是根据现在的互联网环境,还是建议使用https进行连接,使用 HTTPS 发送的数据可通过 TLS得到保护。

​ 因为Hexo本身已经构建的纯静态的博客资源,我便采用熟悉和热门的Nginx代理。

准备证书

证书获取这里不过多赘述,可以自行去域名注册商获取免费的SSL证书,或者用FreeSSL申请免费证书。

证书签发后选择你的web程序类型下载Tomcat、Apache、Nginx或者其他的,我这里就选择Nginx了。下载到本地就是两个文件xxxx.pemxxxx.key,一个是证书文件,一个是证书私钥,或许你下载的是一个.crt.key.crt同样是证书文件只是用了不同的编码方式。

安装Nginx

因为是Centos环境直接就yum方式安装

[root@centos]# yum install -y nginx

whereis nginx可以看见nginx默认目录等

[root@centos hexoo]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz /usr/share/man/man3/nginx.3pm.gz
[root@centos hexoo]#

修改Nginx配置

怕配错可以先备份一个,然后开始编辑

[root@centos]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

[root@centos]# vim /etc/nginx/nginx.conf

需要修改以下配置,默认https 443端口的配置注释掉了,需要手动取消

server_name  kangcunhua.com;  ##你的域名
root         /opt/hexoo/public;   ##你的网站根目录,在hexo目录下执行 hexo g 就会生成一个静态文件的public目录

ssl_certificate "/opt/kangcunhua.com_chain.crt";  ##你的.crt或.pem证书文件的绝对路径
ssl_certificate_key "/opt/kangcunhua.com_key.key";  ##你的证书私钥文件的绝对路径

并在https的 server 段内添加以下配置,能更好的检索根目录的静态文件,注意root段的路径也是网站根目录

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ {
                root /opt/hexoo/public;
                access_log   off;
                expires      30s;
        }
location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
                root /opt/hexoo/public;
                access_log   off;
                expires      30s;
        }
location / {
                   root /opt/hexoo/public;
                   if (-f $request_filename) {
                rewrite ^/(.*)$  /$1 break;
        }

下图是原来的配置和修改后的配置,看看对比

配置完成后可以检查下配置文件是否有问题

[root@centos hexoo]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos hexoo]#

重载nginx配置文件

[root@centos]# systemctl reload nginx

开启强制Https

在http 80的 server段添加跳转配置

return      301 https://$server_name$request_uri;

重载nginx配置文件

[root@centos]# systemctl reload nginx
哔哔(0)
暂无评论,快来抢沙发