Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup proxy using Vercel

Tags:

proxy

vercel

My API it's running under another domain.. and I'm trying to configure proxy with Vercel ..

The app it's making requests to /api/test.json so I tried to... on vercel configuration

"redirects": [
        {
            "source": "/api/test.json",
            "destination": "https://myapi.com/test.json",
        }
    ],
    "rewrites": [
        {
            "source": "/(.*)",
            "destination": "/index.html"
        }
    ]

And I received 404 from /api/test.json

like image 496
ridermansb Avatar asked Jul 18 '26 19:07

ridermansb


2 Answers

Use the wildcard path matching :path* syntax:

// 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*/"
    }
  ]
}

NOTE: You need both very identical objects under rewrites array as above in order to make things work properly. The example in the documentation is only the one without the trailing slash and it won't convert (for example) /api/auth/login/ to https://backend-endpoint/auth/login/, that example can only convert /api/auth/login to https://backend-endpoint/auth/login (without trailing slash /)

(it took me a day to realize that trailing slash / is actually very important).

like image 131
Loi Nguyen Huynh Avatar answered Jul 22 '26 13:07

Loi Nguyen Huynh


Simply use rewrites

"rewrites": [
    {
        "source": "/api/test.json",
        "destination": "https://myapi.com/test.json",
    }
]

Then in your application

httpAgent
  .get('/api/test.json)
  .then(res => { console.log(res) })
like image 26
Bergur Avatar answered Jul 22 '26 15:07

Bergur