hexo配置SSL

修改server.js

因为hexo是直接使用node.js来启动的服务端,并不能像nginx一样去更新SSL证书,那么就需要修改生成的文件$PWD/node_modules/hexo-server/lib/server.js

修改后的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
'use strict';

const connect = require('connect');
const http = require('http');
const { underline } = require('picocolors');
const Promise = require('bluebird');
const open = require('open');
const net = require('net');
const https = require("https");
const fs = require("fs");

module.exports = function(args) {
const app = connect();
const { config } = this;
const ip = args.i || args.ip || config.server.ip || undefined;
const port = parseInt(args.p || args.port || config.server.port || process.env.port, 10) || 4000;
const { root } = config;

const options = {
key:fs.readFileSync("$PWD/xxx.key"),
cert:fs.readFileSync("$PWD/xxx.pem")
};



return checkPort(ip, port).then(() => this.extend.filter.exec('server_middleware', app, {context: this})).then(() => {
if (args.s || args.static) {
return this.load();
}

return this.watch();
}).then(() => startServer(https.createServer(options,app), port, ip)).then(server => {
const addr = server.address();
const addrString = formatAddress(ip || addr.address, addr.port, root);

this.log.info('Hexo is running at %s . Press Ctrl+C to stop.', underline(addrString));
this.emit('server');

if (args.o || args.open) {
open(addrString);
}

return server;
}).catch(err => {
switch (err.code) {
case 'EADDRINUSE':
this.log.fatal(`Port ${port} has been used. Try other port instead.`);
break;

case 'EACCES':
this.log.fatal(`Permission denied. You can't use port ${port}.`);
break;
}

this.unwatch();
throw err;
});
};

//code

主要修改的位置在于

1
2
3
4
5
6
7
8
9
10
11
const https = require("https");
const fs = require("fs");


const options = {
key:fs.readFileSync("$PWD/xxx.key"),
cert:fs.readFileSync("$PWD/xxx.pem")
};

return this.watch();
}).then(() => startServer(https.createServer(options,app), port, ip)).//...