首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Vercel设置代理

如何使用Vercel设置代理
EN

Stack Overflow用户
提问于 2020-12-26 21:06:45
回答 2查看 1.5K关注 0票数 2

我的API它在另一个域下运行..我正在尝试用Vercel配置代理..

它向/api/test.json发出请求的应用程序,所以我试着...关于vercel配置

代码语言:javascript
复制
"redirects": [
        {
            "source": "/api/test.json",
            "destination": "https://myapi.com/test.json",
        }
    ],
    "rewrites": [
        {
            "source": "/(.*)",
            "destination": "/index.html"
        }
    ]

我收到了来自/api/test.json的404

EN

回答 2

Stack Overflow用户

发布于 2021-01-27 22:40:41

只需使用重写

代码语言:javascript
复制
"rewrites": [
    {
        "source": "/api/test.json",
        "destination": "https://myapi.com/test.json",
    }
]

然后在你的应用程序中

代码语言:javascript
复制
httpAgent
  .get('/api/test.json)
  .then(res => { console.log(res) })
票数 2
EN

Stack Overflow用户

发布于 2021-07-03 06:02:28

使用wildcard path matching :path*语法:

代码语言:javascript
复制
// in vercel.json:
// for example proxying /api/ → https://backend-endpoint/

{
  "rewrites": [
    {
      "source": "/api/:path*",
      "destination": "https://backend-endpoint/:path*"
    },
    {
      "source": "/api/:path*/",
      "destination": "https://backend-endpoint/:path*/"
    }
  ]
}

注意:为了让事情正常工作,你需要在上面的rewrites 数组下使用两个完全相同的对象。documentation中的示例只是一个没有尾随斜杠的示例,它不会将(例如) /api/auth/login/转换为https://backend-endpoint/auth/login/,该示例只能将/api/auth/login转换为https://backend-endpoint/auth/login (没有尾随斜杠/)

(我花了一天时间才意识到尾随斜杠/实际上非常重要)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65456701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档