2.3 KiB
2.3 KiB
部署问题修复记录
问题描述
部署后访问 http://115.190.235.230:8006 时出现严重的重定向循环问题:
- URL被重复拼接多次:
http://115.190.235.230:8006/115.190.235.230:8006/115.190.235.230:8006/... - 浏览器报错:
ERR_TOO_MANY_REDIRECTS - Service Worker 不被支持警告
根本原因
Next.js 配置文件中使用了 output: 'standalone' 配置,但部署时使用的是 next start 命令启动,两者不兼容导致:
- Next.js 警告:
"next start" does not work with "output: standalone" configuration - 路由处理异常,导致URL重复拼接
- 重定向循环
解决方案
1. 修改 Next.js 配置
文件: next.config.mjs
修改前:
const nextConfig = {
output: 'standalone', // 问题所在
images: {
unoptimized: true,
},
}
修改后:
const nextConfig = {
images: {
unoptimized: true,
},
}
2. 清理服务器缓存并重新构建
# 连接服务器
ssh -p 22 root@115.190.235.230
# 进入项目目录
cd /var/www/chinese-family-tree
# 停止应用
pm2 stop chinese-family-tree
# 删除旧的构建文件
rm -rf .next
# 重新构建
pnpm build
# 重启应用
PORT=8006 pm2 restart chinese-family-tree --update-env
3. 验证修复
# 检查HTTP响应
curl -I http://115.190.235.230:8006/
# 应该返回: HTTP/1.1 200 OK
# 检查应用日志
pm2 logs chinese-family-tree --lines 20
# 不应该再有 standalone 警告
修复结果
✅ 重定向循环问题已解决 ✅ 应用正常启动在 8006 端口 ✅ 页面可以正常访问 ✅ 没有 standalone 配置警告
经验教训
-
不要混用 standalone 和 next start
output: 'standalone'需要使用node .next/standalone/server.js启动- 使用
next start时不要配置output: 'standalone'
-
部署后清理缓存
- 修改配置后必须删除
.next目录重新构建 - 否则会使用旧的缓存文件
- 修改配置后必须删除
-
验证环境变量
- 确保
NEXTAUTH_URL与实际访问URL一致 - 使用
pm2 env <id>检查运行时环境变量
- 确保