From 6fa4f5713cd144eed4c19f630ac508cee7856445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=83=E5=AE=87?= Date: Tue, 23 Apr 2024 16:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9B=B8=E5=85=B3=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=B8=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 98 +++++++++++++++++++++++++++++++++++++++---------- pm2.json | 15 ++++++++ server/index.js | 7 +++- 3 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 pm2.json diff --git a/README.md b/README.md index 614b9dd9..a1b075dd 100644 --- a/README.md +++ b/README.md @@ -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 // 实例数 + }] +} +``` diff --git a/pm2.json b/pm2.json new file mode 100644 index 00000000..79182020 --- /dev/null +++ b/pm2.json @@ -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 + }] +} \ No newline at end of file diff --git a/server/index.js b/server/index.js index 58d11e66..d11c84eb 100644 --- a/server/index.js +++ b/server/index.js @@ -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));