Content refreshed · every 10 min
nextjsaliyun-fcdockerserverlessdeploymentdevops
Next.js 部署到阿里云 FC 3.0 实战指南
S
Schwarz
Article
Next.js 部署到阿里云 FC 3.0 实战指南
Next.js 部署到阿里云 FC 3.0 实战指南
以 sesefg (Next.js 16) 为例,记录从本地开发到 FC 3.0 上线的完整流程,包括踩坑与优化。
📋 概述
将一个基于 Next.js 16 (App Router + Standalone) 的项目部署到阿里云函数计算 FC 3.0,通过自定义域名对外提供服务。
最终效果:
- 网站:
https://www.yuntun-bj.com - ISR 重新验证:
POST /api/revalidate
关键技术栈:
- Next.js 16 — App Router,
output: 'standalone' - Docker — 容器化部署
- 阿里云 FC 3.0 — Serverless 运行时
- 阿里云自定义域名
🏗️ 项目结构
sesefg/
├── src/
│ ├── app/ # Next.js App Router 页面
│ ├── components/ # React 组件
│ ├── lib/ # 工具库 (API、i18n、SEO)
│ └── types/ # TypeScript 类型
├── Dockerfile # 生产部署用(本地 build 版)
├── Dockerfile.fc # 全容器 build 版(备选)
├── next.config.ts # Next.js 配置
└── package.json
next.config.ts 关键配置:
const nextConfig: NextConfig = {
output: 'standalone', // 必须开启
images: {
formats: ['image/avif', 'image/webp'],
remotePatterns: [{ protocol: 'https', hostname: '**' }],
},
}
🚧 踩坑记录
1. Docker 镜像架构不匹配
问题: Mac M 系列 (arm64) 默认构建 arm64 镜像,FC 实例是 amd64。
exec format error (code 8)
解决: 必须指定 --platform linux/amd64
2. Native Modules 架构问题
问题: @swc/* 等原生模块在跨架构时不兼容。
方案 A(推荐): 本地 pnpm build 后 Dockerfile 只做 COPY,30 秒完成。
方案 B: 在 amd64 容器内完整 build,约 4 分钟。
3. FC 函数名与域名绑定
问题: 用 s CLI 创建函数名 web,但域名绑定的是 sesefg。一直更新错误函数。
排查: aliyun fc-open GET "/2021-04-06/custom-domains"
4. ISR 在 FC 上不持久
问题: FC 实例被回收后 ISR 缓存丢失。
解决: 通过 Revalidate API 手动触发,或接受 fresh SSR。
✅ 正确部署流程
Step 1: 本地构建
cd sesefg && pnpm build
Step 2: 构建并推送 Docker 镜像
docker buildx build --platform linux/amd64 -t alichs/sesefg:latest --push .
Step 3: 获取镜像 Digest
DIGEST=$(docker buildx imagetools inspect alichs/sesefg:latest 2>&1 | grep "Digest:" | awk '{print $2}')
Step 4: 更新 FC 函数
aliyun fc UpdateFunction --region ap-northeast-1 \
--functionName sesefg \
--body "{\"customContainerConfig\":{\"image\":\"docker.io/alichs/sesefg@${DIGEST}\",\"port\":3000}}"
Step 5: 验证
sleep 20 && curl -sL -o /dev/null -w "HTTP %{http_code}\\n" https://www.yuntun-bj.com/
🔄 日常更新流程
pnpm build
docker buildx build --platform linux/amd64 -t alichs/sesefg:latest --push .
DIGEST=$(docker buildx imagetools inspect alichs/sesefg:latest | grep Digest | awk '{print $2}')
aliyun fc UpdateFunction --region ap-northeast-1 --functionName sesefg \
--body "{\"customContainerConfig\":{\"image\":\"docker.io/alichs/sesefg@${DIGEST}\",\"port\":3000}}"
内容更新不需要重新部署,调用 Revalidate API 即可。
💰 成本估算
| 配置项 | 值 |
|---|---|
| CPU | 1 核 |
| 内存 | 2048MB |
| 冷启动 | ~10-20 秒 |
| 热请求 | ~1-3 秒 |
| 月预估 | < ¥20 |
🔑 关键决策
| 决策点 | 选择 | 原因 |
|---|---|---|
| FC 版本 | 3.0 | 支持 Docker Hub |
| 区域 | ap-northeast-1 | 东京,延迟低 |
| 构建策略 | 本地 build + COPY | 30秒 vs 4分钟 |
| 镜像仓库 | Docker Hub | 免费 |
文档生成时间:2026-05-04 维护者:Schwarz