Skip to content

fix: 将环境变量迁移到 runtimeConfig,修复生产环境环境变量不生效问题#37

Merged
ikxin merged 5 commits intomainfrom
copilot/check-api-url-and-port-issue
Mar 18, 2026
Merged

fix: 将环境变量迁移到 runtimeConfig,修复生产环境环境变量不生效问题#37
ikxin merged 5 commits intomainfrom
copilot/check-api-url-and-port-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 18, 2026

生产服务器(node .output/server/index.mjs)不会自动加载 .env 文件,且 MONITOR_LIST/ENABLE_VLMCSD/MONITOR_INTERVAL 未注册到 Nuxt runtimeConfig,导致这些变量在生产环境始终无法生效。此外,-p 端口参数对 Nitro 无效,需通过 PORT 环境变量修改。

这个变动的性质是

  • 新特性提交
  • 日常 bug 修复
  • 文档改进
  • 组件样式改进
  • 重构
  • 代码风格优化
  • 分支合并
  • 其他改动(是关于什么的改动?)

需求背景

生产部署(宝塔面板等)中设置 .env 后重新构建,MONITOR_LIST 监控列表和 API 地址仍不生效。根本原因:

  1. Nitro Node.js server 在生产模式下不加载 .env,环境变量必须在启动进程前注入系统环境
  2. MONITOR_LIST/ENABLE_VLMCSD/MONITOR_INTERVAL 直接读取 process.env,未进入 Nuxt 的 runtimeConfig 体系,无法通过 applyEnv() 在服务启动时正确应用

实现方案和 API(非新功能可选)

核心修复:MONITOR_LIST/ENABLE_VLMCSD/MONITOR_INTERVAL 全部注册进 runtimeConfig,使 Nitro 的 applyEnv() 在服务启动时自动将 NUXT_* 前缀环境变量注入配置。

  • nuxt.config.tsruntimeConfig 新增 monitorList: ''enableVlmcsd: ''monitorInterval: '10',触发 NUXT_MONITOR_LIST / NUXT_ENABLE_VLMCSD / NUXT_MONITOR_INTERVAL 的自动映射;getMonitorCron() 函数优先读取 NUXT_MONITOR_INTERVAL,回退 MONITOR_INTERVAL(向后兼容)
  • server/utils/kms.tsmonitorList 常量改为带缓存的 getMonitorList() 函数,优先读取 useRuntimeConfig().monitorList,回退 process.env.MONITOR_LIST(向后兼容)
  • server/plugins/vlmcsd.ts:改用 useRuntimeConfig().enableVlmcsd,回退 process.env.ENABLE_VLMCSD
  • server/api/monitor.tsserver/tasks/monitor.ts:改用 getMonitorList()

启动命令示例(端口修改同样适用):

PORT=3512 NUXT_MONITOR_LIST=kms.example.com NUXT_MONITOR_INTERVAL=30 node .output/server/index.mjs

Note

MONITOR_INTERVALNUXT_MONITOR_INTERVAL)用于控制 scheduled task 的 cron 表达式,该值在构建时确定并编译进产物,修改后需重新 build 生效。

文档:

  • .env.example 更新为 NUXT_MONITOR_LIST / NUXT_ENABLE_VLMCSD / NUXT_MONITOR_INTERVAL
  • README.md / README.en.md 更新变量名表格,新增 PORTNUXT_MONITOR_INTERVAL 说明,新增生产环境不加载 .env 的注意事项

对用户的影响和可能的风险(非新功能可选)

  • 环境变量名变更MONITOR_LISTNUXT_MONITOR_LISTENABLE_VLMCSDNUXT_ENABLE_VLMCSDMONITOR_INTERVALNUXT_MONITOR_INTERVAL。旧名称仍作为 fallback 保留,不强制迁移,向后兼容
  • Docker Compose 示例中 MONITOR_LIST 同步更新为 NUXT_MONITOR_LIST

Changelog 描述(非新功能可选)

  1. Fix: MONITOR_LIST, ENABLE_VLMCSD, and MONITOR_INTERVAL now properly registered in Nuxt runtimeConfig; use NUXT_MONITOR_LIST / NUXT_ENABLE_VLMCSD / NUXT_MONITOR_INTERVAL in production. Old names remain as fallback. Added PORT and NUXT_MONITOR_INTERVAL env var documentation.
  2. 修复:MONITOR_LISTENABLE_VLMCSDMONITOR_INTERVAL 已迁移至 Nuxt runtimeConfig,生产环境请使用 NUXT_MONITOR_LIST / NUXT_ENABLE_VLMCSD / NUXT_MONITOR_INTERVAL,旧变量名作为回退仍可用。新增 PORTNUXT_MONITOR_INTERVAL 环境变量文档说明。

请求合并前的自查清单

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • Changelog 已提供或无须提供

后续计划(非新功能可选)

无。

Original prompt

This section details on the original issue you should resolve

<issue_title>关于端口和API的问题</issue_title>
<issue_description>我使用的宝塔面板的 Node 模块构建并上线的服务

出现以下问题 不知道如何解决 特此来请教

我在 Git 后将 kms-tools-main 中的 .env.example 文件更名为 .env 文件,并将其中的内容改为

NUXT_PUBLIC_API_URL=https://web.site
NUXT_PUBLIC_I18N_BASE_URL=https://web.site
MONITOR_LIST=kms.web.site
ENABLE_VLMCSD=true

然后通过宝塔的 Node 模块 Build 项目后,通过自定义启动命令 node .output/server/index.mjs 成功上线服务

  1. 我在自定义命令后 添加 -p 3512 命令后,默认端口 3000 没改变,请教一下如何更改

  2. 在更改了 .env 文件后,上述 API 地址、监控列表没有改变,请教一下!</issue_description>

<agent_instructions>排查当前仓库最新的代码是否还存在该问题</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@ikxin @LeticiaAnna 端口问题请参阅 https://nuxt.zhcndoc.com/docs/4.x/getting-started/deployment#%E5%85%A5%E5%8F%A3%E7%82%B9

至于 MONITOR_LIST 未生效,想问一下你是先设置 env 再 build 的吗?</comment_new>
<comment_new>@ikxin
看起来像是没有读取到环境变量,可以截图看一下你的项目配置吗

Image</comment_new>
<comment_new>@ikxin
@LeticiaAnna 我现在没有可以用于测试的宝塔环境,你看看能不能借助 AI 排查一下,问题应该是出在环境变量的设置上,我感觉是宝塔的 Node 项目没有读取到 .env 的内容</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 18, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
kms-tools Ready Ready Preview, Comment Mar 18, 2026 5:10am

@netlify
Copy link
Copy Markdown

netlify bot commented Mar 18, 2026

Deploy Preview for kms-tools ready!

Name Link
🔨 Latest commit 4b52ea4
🔍 Latest deploy log https://app.netlify.com/projects/kms-tools/deploys/69ba339a06281e000849a5fd
😎 Deploy Preview https://deploy-preview-37--kms-tools.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix API URL and port configuration issues fix: 将 MONITOR_LIST/ENABLE_VLMCSD 迁移到 runtimeConfig,修复生产环境环境变量不生效问题 Mar 18, 2026
Copilot AI requested a review from ikxin March 18, 2026 04:19
@ikxin
Copy link
Copy Markdown
Owner

ikxin commented Mar 18, 2026

@copilot 解决最新代码的冲突

Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot 解决最新代码的冲突

已在 9d52984 中合并主分支的冲突。将 main 分支新增的 MONITOR_INTERVAL 支持(getMonitorCron() 函数及 .env.example/README 中的对应条目)合并进本 PR 的变更中,两组修改现已共存。

@ikxin ikxin marked this pull request as ready for review March 18, 2026 04:35
@ikxin
Copy link
Copy Markdown
Owner

ikxin commented Mar 18, 2026

@copilot 你需要将 #38 的环境变量也加入进来,然后解决冲突

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot 你需要将 #38 的环境变量也加入进来,然后解决冲突

PR #38 的变更(MONITOR_INTERVALgetMonitorCron() 函数及对应 README 行)已在上一次提交(9d52984)中合并进来,冲突已解决。当前分支与 main 的差异仅包含本 PR 的核心修复:runtimeConfig 新增 monitorList/enableVlmcsd.env.example 更新为 NUXT_MONITOR_LIST/NUXT_ENABLE_VLMCSDPORT 文档及服务端代码变更,MONITOR_INTERVAL 保持不变。

@ikxin
Copy link
Copy Markdown
Owner

ikxin commented Mar 18, 2026

@copilot MONITOR_INTERVAL 也需要在 Nuxt 运行时读取呀

…ONITOR_INTERVAL)

Co-authored-by: ikxin <54543761+ikxin@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot MONITOR_INTERVAL 也需要在 Nuxt 运行时读取呀

已在 f31ae34 中处理:

  • nuxt.config.tsruntimeConfig 新增 monitorInterval: '10',对应 NUXT_MONITOR_INTERVAL 环境变量
  • getMonitorCron() 在构建时优先读取 NUXT_MONITOR_INTERVAL,回退 MONITOR_INTERVAL(向后兼容)
  • .env.example 和 README 表格同步更新为 NUXT_MONITOR_INTERVAL

注意:scheduled task 的 cron 表达式在构建时确定,修改 NUXT_MONITOR_INTERVAL 后需要重新 build 才能生效(与 Nitro 的 scheduledTasks 机制一致)。

Copilot AI changed the title fix: 将 MONITOR_LIST/ENABLE_VLMCSD 迁移到 runtimeConfig,修复生产环境环境变量不生效问题 fix: 将 MONITOR_LIST/ENABLE_VLMCSD/MONITOR_INTERVAL 迁移到 runtimeConfig,修复生产环境环境变量不生效问题 Mar 18, 2026
@ikxin ikxin merged commit 28ff069 into main Mar 18, 2026
5 of 7 checks passed
@ikxin ikxin changed the title fix: 将 MONITOR_LIST/ENABLE_VLMCSD/MONITOR_INTERVAL 迁移到 runtimeConfig,修复生产环境环境变量不生效问题 fix: 将环境变量迁移到 runtimeConfig,修复生产环境环境变量不生效问题 Mar 18, 2026
@ikxin ikxin deleted the copilot/check-api-url-and-port-issue branch March 18, 2026 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

关于端口和API的问题

2 participants