0%

Nginx 部署

将发布Blog的云服务器交由nginx代理

  • 对外端口由nginx进行监听

  • hexo server运行于本地,由nginx进行反向代理

  • php-fpm运行于本地,由nginx进行反向代理

nginx代理php

给live2d提供模型数据接口以及资源的php已由 nginx代理。php服务器监听本地9000端口。

测试nginx可配置分离,根目录下指定监听端口,通用配置等

根目录配置如下

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
# 指定跨域,反向代理的配置用include指定
http {

server {
listen 80;
server_name loolob

# 设置跨域配置 Start
set $cors_origin "";
if ($http_origin ~* "^http://localhost$"){
set $cors_origin $http_origin;
}

add_header Access-Control-Allow-Origin http://loolob.cn;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,x-auth-token always;
add_header Access-Control-Max-Age 1728000 always;

# 预检请求处理
if ($request_method = OPTIONS) {
return 204;
}
# #设置跨域配置 End

include /root/loolob_blog/live2d_api/nginx.conf;
}
}

php代理配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 匹配/live2d_api路径,并在/root/loolob_blog目录下寻找文件
# 实际的文件在/root/loolob_blog/live2d_api/目录下
location /live2d_api/ {
root /root/loolob_blog/;
index index.php index.html index.htm;
}

# 匹配所有以.php结尾的文件
location ~ \.php$ {
root /root/loolob_blog/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

nginx代理hexo

改造hexo server启动命令

将hexo server进程交给pm2进行管理,为此需要手动添加一个中间脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const { fork } = require('child_process')

// hexo server监听地址localhost:47777,不直接对外
const hexo_server = fork("./node_modules/hexo/bin/hexo", ["s", "-i", "localhost", "-p", "47777", "-l"]);

hexo_server.on('message', function (m) {
console.log('message from hexo server:' + m);
});

// hexo server异常关闭时,关闭此中间脚本
// 由于此脚本托管给了pm2,pm2会自动重启非主动关闭的脚本
hexo_server.on('close', function () {
process.exit()
})

process.on('message', function (m) {
console.log('message from pm2: ' + m);
});

// 监听来自pm2中断消息,当pm2关闭时,同时关闭hexo server进程
// 防止pm2停止此中间脚本了,但hexo server还在运行的情况
process.on('SIGINT', function () {
hexo_server.kill('SIGINT')
});

添加nginx对hexo server的代理

1
2
3
4
5
6
# 直接转发
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost.com:47777;
}