Vercel 405 Method Not Allowed

📅 2026/7/12 4:12:55 👁️ 阅读次数 📝 编程学习
Vercel 405 Method Not Allowed

✅ Vercel 405 Method Not Allowed 已修复

根本原因

Vercel 的外部重写 (${BACKEND_URL}) 不支持 POST/PUT/DELETE 方法,只支持 GET。所以当POST /api/auth/register触发重写时,Vercel 返回 405 错误。这是 Vercel 的已知限制。

解决方案

使用Vercel Serverless 函数作为代理,代替不可用的重写功能:

变更文件说明
apps/frontend/api/proxy/[[...path]].ts新增:通配 Serverless 代理函数,正确转发所有 HTTP 方法(GET/POST/PUT/DELETE/PATCH/OPTIONS)
apps/frontend/vercel.json移除${BACKEND_URL}外部重写(已不可用)
.github/workflows/ci-cd.ymlVITE_API_URL改为/api/proxyBACKEND_URL指向实际后端地址

新的请求链路

前端 /api/proxy/auth/register → Vercel Serverless Function (api/proxy/[[...path]].ts) → fetch(BACKEND_URL/auth/register, { method: 'POST', body: ... }) → 后端处理并返回 → Serverless 返回给前端(带 CORS headers)

Git 操作

步骤状态
提交到 main16a480a
推送到远程✅ Done
触发 CI/CD✅ 自动触发

等待 CI/CD 完成后,POST /api/auth/register将不再返回 405 错误。