更新相关配置与文档

This commit is contained in:
黄心宇 2024-04-23 16:21:05 +08:00
parent 81c1fa95ae
commit 6fa4f5713c
3 changed files with 99 additions and 21 deletions

View File

@ -2,37 +2,97 @@
#### node版本
12.x
12.x/14.x
#### ng配置
后端服务另分配一个端口,不再映射域名
改为前端服务映射域名并转发到3000
```
if ($http_user_agent ~* "googlebot|Mediapartners-Google|bingbot|google-structured-data-testing-tool|baiduspider|360Spider|Sogou Spider|Yahoo! Slurp China|^$")
{#判断如果是网络爬虫转发到node服务器下
proxy_pass http://www.test.com:3000;
break;
}
server {
listen 8080;
server_name testforgeplus.trustie.net;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 8081;
# server_name testforgeplus.trustie.net;
# return http://$server_name$request_uri;
# rewrite ^(.*)$ https://$host$1 permanent;
...
}
```
#### 依赖安装、运行
#### 部署脚本
```bash
npm install pm2 -g //装过就不用再执行此条
#!/bin/bash
// 依赖
set -x
# Avoid root execution
if [ `id|sed -e s/uid=//g -e s/\(.*//g` -eq 0 ]; then
echo "Execution by root not allowed"
exit 1
fi
cd
#上线分支
Branch=pre_gitlink_ssr
#代码路径
Code_Path=/home/pdl/forgeplus/public/react/forgeplus-react
#Build代码路径
Build_Path=/home/pdl/forgeplus/public/react
cd $Code_Path
pwd
git checkout $Branch
git pull
# 分别打静态包与node服务包
npm install
// 打包
npm run build
npm run build:server
// 启动服务
npm run pm2
# 复制一份到原来build路径
cd $Build_Path
rm -rf build.bak
mv build build.bak
scp -r $Code_Path/build ./
```
或者
```bash
npm install pm2 -g //装过就不用再执行此条
./build.sh
pm2 restart $Code_Path/pm2.json
# 重新加载 Nginx 配置
/home/pdl/nginx/sbin/nginx -s reload
# 检查 Nginx 是否重启成功
if [ "$?" -eq "0" ]; then
echo -e "\e[1;42m restart nginx OK! \e[0m"
else
echo -e "\e[1;31m restart nginx FAILED! \e[0m"
fi
```
pm2.json配置
```json
{
"apps": [{
"name": "gitlink-ssr",
"script": "buildserver/bundle.js",
"watch": false,
"log_date_format": "YYYY-MM-DD HH:mm Z",
"exec_mode": "cluster",
"max_memory_restart": "300M", // 内存超过300m重启
"env": {
"NODE_ENV": "production",
"PORT": "8081" // 修改为ng给后端服务另配的端口
},
"instances": 4 // 实例数
}]
}
```

15
pm2.json Normal file
View File

@ -0,0 +1,15 @@
{
"apps": [{
"name": "gitlink-ssr",
"script": "buildserver/bundle.js",
"watch": false,
"log_date_format": "YYYY-MM-DD HH:mm Z",
"exec_mode": "cluster",
"max_memory_restart": "300M",
"env": {
"NODE_ENV": "production",
"PORT": "8081"
},
"instances": 4
}]
}

View File

@ -6,11 +6,14 @@ import { url, zoneUrl } from "../src/ssrUrl";
const { createProxyMiddleware } = require('http-proxy-middleware');
import Logger from "./log";
// 转发的后端服务的端口
const rubyPort = process.env.PORT || 8081
const app = express();
app.use('/build', express.static('build'));
// 处理其他静态文件
app.use(express.static(path.resolve(__dirname, '/home/pdl/forgeplus/public')));
app.use(express.static(path.resolve('../')));
const options = (target) => {
return {
@ -29,7 +32,7 @@ const options = (target) => {
};
}
const proxy = createProxyMiddleware(options('http://localhost:8081'));
const proxy = createProxyMiddleware(options(`http://localhost:${ rubyPort }`));
// const proxy = createProxyMiddleware(options(url));