Next.js 部署到阿里云 FC 3.0 实战指南
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,通过自定义域名对外提供服务。
最终效果:
关键技术栈:
- Next.js 16 — App Router, output: standalone
- Docker — 容器化部署
- 阿里云 FC 3.0 — Serverless 运行时
- Docker Hub — 镜像仓库
🏗️ 项目结构
sesefg/
├── src/
│ ├── app/ # Next.js App Router 页面
│ ├── components/ # React 组件
│ ├── lib/ # 工具库
│ └── types/ # TypeScript 类型
├── Dockerfile # 生产部署用(本地 build 版)
├── Dockerfile.fc # 全容器 build 版(备选)
├── next.config.ts
└── 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。
解决: docker buildx build --platform linux/amd64
2. Native Modules 架构问题
@swc 等原生模块在跨架构时不兼容。
推荐方案: 本地 pnpm build 后 Dockerfile 只做 COPY,30 秒完成。
备选方案: 在 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: 本地构建 pnpm build
Step 2: 构建并推送镜像 docker buildx build --platform linux/amd64 -t alichs/sesefg:latest --push .
Step 3: 获取 Digest DIGEST=$(docker buildx imagetools inspect alichs/sesefg:latest | 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: 验证 curl -sL -o /dev/null -w '%{http_code}' https://www.yuntun-bj.com/
🔄 日常更新
代码修改后三步:build → push → update FC
内容更新不需要重新部署,调用 Revalidate API 即可。
💰 成本
CPU 1核 / 内存 2048MB / 冷启动 ~10-20秒 / 热请求 ~1-3秒 / 月预估 < ¥20
🔑 关键决策
- FC 3.0:支持 Docker Hub,不需要 ACR
- 区域 ap-northeast-1(东京)
- 本地 build + COPY(30秒 vs 容器内 4分钟)
- Docker Hub(免费)
文档生成时间:2026-05-04 | 维护者:Schwarz