一、windows服务器环境安装
1、IIS 必须安装两个模块
URL Rewrite Module 2.1https://download.microsoft.com/download/1/2/8/128E2E22-C1B9-44A4-BE2A-5859ED1D4592/rewrite_amd64_zh-CN.msi
ARR 3.0 应用程序请求路由
https://go.microsoft.com/fwlink/?LinkID=615136
安装好之后,开启 ARR 代理功能。在IIS根部点ARR,在右边点Server Proxy Setting,勾选 启用代理 (Enable proxy),取消勾选:Reverse rewrite host in response headers(「保留主机头」开关)
下面会提到在 web.config 添加<allowedServerVariables>变量,老版本 IIS/ARR 有全局锁,需要解除锁节点。否则添加了变量后网页会报500错误。在IIS根部点配置编辑器,选中如下节点后,点解除锁节点
新建站点域名,并指向 D:\web\as.chngoodcar.com
2、安装 Node.js LTS(推荐 20.x,添加到系统 PATH)
https://nodejs.org/dist/v24.21.0/node-v24.21.0-x64.msi
node -v
npm -v
3、安装 PM2,用 PM2 常驻 Node
npm install pm2 -g
二、服务器上构建
1、 从git获取代码
#1、先安装Git
https://github.com/git-for-windows/git/releases/download/v2.56.0.windows.2/Git-2.56.0.2-64-bit.exe
#2、把代码拉取到本地
cd /d D:\web\as.chngoodcar.com
git clone https://github.com/Blue-h-whale/goodCar.git
2、安装依赖并构建
cd D:\web\as.chngoodcar.com\goodCar\模板一
#1、安装依赖
npm ci
set NUXT_PUBLIC_SITE_URL=https://as.chngoodcar.com
set NUXT_PUBLIC_API_BASE_URL=https://api.chngoodcar.com/api
set NODE_OPTIONS=--max-old-space-size=8192
#2、Nuxt 项目打包
npm run build
打包完成后,.output\server\index.mjs 文件即存在。就可以用 PM2 启动index.mjs
3、服务器上本地冒烟测试
set HOST=127.0.0.1
set PORT=3000
set NITRO_HOST=127.0.0.1
set NITRO_PORT=3000
node .output\server\index.mjs
浏览器访问 http://127.0.0.1:3000,确认首页可开后 Ctrl+C 结束。
4、用 PM2 常驻 Node(推荐)
在站点根目录创建 ecosystem.config.cjs:
module.exports = {
apps: [
{
name: 'as-chngoodcar',
cwd: 'D:/web/as.chngoodcar.com/goodCar/模板一',
script: '.output/server/index.mjs',
instances: 1,
exec_mode: 'fork',
env: {},
// 额外增加日志配置,强制输出日志
out_file: "./pm2-out.log",
error_file: "./pm2-err.log"
}
]
}
为什么env保持为空,是因为部分版本 pm2 传给 node 的环境变量不稳定。
在D:\web\as.chngoodcar.com\goodCar\模板一根目录下,新建.env文件。先建立一个文本文件,另存为.env,保持类型:所有文件。编码:UTF-8。文件内容如下:
NODE_ENV=production
HOST=127.0.0.1
PORT=3000
NITRO_HOST=127.0.0.1
NITRO_PORT=3000
NUXT_PUBLIC_SITE_URL=https://as.chngoodcar.com
NUXT_PUBLIC_API_BASE_URL=https://api.chngoodcar.com/api
启动并保存:
cd /d D:\web\as.chngoodcar.com\goodCar\模板一
pm2 start ecosystem.config.cjs
pm2 save
如要删除使用:
pm2 delete as-chngoodcar
再执行保持。
检查:
pm2 status
状态为online正常,可用浏览器访问http://127.0.0.1:3000
另外提供手动验证的方法做参考(该部分无需操作),打开PowerShell,输入如下:
cd D:/web/as.chngoodcar.com/goodCar/模板一
$env:NODE_ENV="production"
$env:NITRO_HOST="127.0.0.1"
$env:NITRO_PORT=3000
$env:NUXT_PUBLIC_SITE_URL="https://as.chngoodcar.com"
$env:NUXT_PUBLIC_API_BASE_URL="https://api.chngoodcar.com/api"
node .output/server/index.mjs
运行后再浏览器访问http://127.0.0.1:3000
5、开机自动恢复 PM2 Nuxt 项目
抛弃使用pm2-windows-startup(版本过老),也尝试过pm2-installer(失败),尝试注册Windows服务也失败,最终采用windows任务计划程序
WIN+R,输入taskschd.msc
操作截图如下:
程序或脚本:powershell.exe
添加参数 (可选):-WindowStyle Hidden -Command "pm2 resurrect"
确定后,输入Administrator账号的密码即完成。
在任务计划列表,右键PM2_AutoResume→【运行】。
重要前置:(创建任务前必须执行一次)pm2 restart ecosystem.config.cjs && pm2 save更新快照。
pm2 status 查看进程 online,代表任务配置成功,重启后试试是否网站正常运行。
三、IIS设置
设置站点应用程序池
.NET CLR 版本:无托管代码
托管管道模式:集成
启动模式:AlwaysRunning(可选)
根目录新建web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="HTTP_X_FORWARDED_HOST" />
<add name="HTTP_X_FORWARDED_PROTO" />
</allowedServerVariables>
<rules>
<rule name="ReverseProxyToNuxt" stopProcessing="true">
<match url="(.*)" />
<serverVariables>
<set name="HTTP_X_FORWARDED_HOST" value="{HTTP_HOST}" />
<set name="HTTP_X_FORWARDED_PROTO" value="{HTTPS}" />
</serverVariables>
<action type="Rewrite" url="http://127.0.0.1:3000/{R:1}" appendQueryString="true" />
</rule>
<rule name="HTTP to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<httpErrors existingResponse="PassThrough" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
最后就是安装HTTPS 证书,这里就不详细讲了。
四、日常发版流程
cd /d D:\web\as.chngoodcar.com\goodCar\模板一
git pull
rem 或覆盖上传代码后:
npm ci
set NUXT_PUBLIC_SITE_URL=https://as.chngoodcar.com
set NUXT_PUBLIC_API_BASE_URL=https://api.chngoodcar.com/api
set NODE_OPTIONS=--max-old-space-size=8192
npm run build
pm2 restart ecosystem.config.cjs
pm2 save