Skip to content
返回部署

使用 Netlify 部署

基于 Git 的部署,含 serverless 函数、边缘函数和内置表单/身份。

平台入门

为什么

Netlify 提供 git push 部署,原子部署、即时回滚和内置附加功能(表单、身份、分析)。边缘函数运行于 Deno 全球分布。

平台即服务采用率 2024 年增长 24%,团队从虚拟机管理转向 git push 部署。the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research),PaaS 层捕获了大多数新工作负载。

何时使用

静态 + serverless 站点、表单处理和想要内置 auth 无需第三方服务的团队选 Netlify。需要自定义内核模块、GPU 直通或严格合规(FedRAMP、HIPAA)的工作负载避免 PaaS — 改用 IaaS 或专用主机。

如何做

1. 连接仓库 — app.netlify.com → 新建站点 → Git 2. 构建 — 设构建命令 + 发布目录(已知框架自动检测) 3. 函数 — netlify/functions/*.ts 成为 serverless 端点 4. 边缘 — netlify/edge-functions/*.ts 运行于 Deno 全球 5. 部署 — git push,或 npx netlify deploy --prod 6. 重定向 — netlify.toml 或 _redirects 文件 7. 头 — netlify.toml 或 _headers 文件

示例

# netlify.toml [build] command = "npm run build" publish = "dist" [[redirects]] from = "/*" to = "/index.html" status = 200 [[headers]] for = "/*" [headers.values] X-Frame-Options = "DENY" # netlify/functions/api.ts — serverless export default async (req: Request) => Response.json({ ok: true });

常见问题

  • ⚠构建命令未找到 — 发布目录错误或缺构建脚本
  • ⚠函数 404 — 文件夹错误,须 netlify/functions/ 非 functions/
  • ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致

最佳实践

  • ✓用 netlify.toml 而非 _redirects/_headers — 版本控制且更强大
  • ✓先部署预览 — npx netlify deploy,验证后 --prod
  • ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求

常见问题解答