Proxy URI path to external IP

I've recently tried deploying a NextJS site, and with the URL Rewrites not working I need to find an alternative. I'm trying to route /api/:path to an external IP where my API is hosted, but with very little luck. The only thing that has worked so far is setting up a Page Redirect, but I don't want my API's IP address being exposed, and preferably proxied. Any straightforward way to do this?
20 Replies
James
James•14mo ago
You could try using middleware or next.config.js rewrites perhaps
Ethernal
Ethernal•14mo ago
I did try next.config.js rewrites, but they didn't work. thats why im trying to find an alternative And didn't work as in it has the same issues as the Page redirect, which is exposing the origin IP only thing I can really think of at the moment is to setup an api.domain/* instead of domain/api/*
James
James•14mo ago
Are you using @cloudflare/next-on-pages?
Ethernal
Ethernal•14mo ago
Yes When going to <page>.pages.dev/api/:path it redirects me to the actual IP address instead of staying on <page>.pages.dev/api/:path, and im trying to avoid that
James
James•14mo ago
Ah... That's a bug then. Sorry about that - I'll look into a fix
Ethernal
Ethernal•14mo ago
Oh, alright. thank you
James
James•14mo ago
Hey there, sorry again - this was my mistake a couple months back 😓. I've opened a PR, I would welcome you to try out the prerelease in https://github.com/cloudflare/next-on-pages/pull/394#issuecomment-1646659126
Ethernal
Ethernal•14mo ago
Ok, I'll go ahead and try that. thank you! Hm, it's still redirecting me
James
James•14mo ago
Can you please confirm that you're using the prerelease in that PR. It will say the version at the start of the next-on-pages build.
Ethernal
Ethernal•14mo ago
I followed in installation guide on the PR, and my package.json reports
"@cloudflare/next-on-pages": "https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/5632432242/npm-package-next-on-pages-394",
"@cloudflare/next-on-pages": "https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/5632432242/npm-package-next-on-pages-394",
and then I pushed it to git and cloudflare redeployed the site
James
James•14mo ago
If your build command in the pages project settings is npx @cloudflare/next-on-pages@1 with the @1 part, I think it will ignore the prerelease version, so could you just check that
Ethernal
Ethernal•14mo ago
Ah- I had missed that part in the documentation, My build command is still next build FacePalm
James
James•14mo ago
Your package.json build script should be next build, and the default build command in the pages project settings on the dashboard is npx @cloudflare/next-on-pages@1 when creating a new non-static Next.js pages project 🙂
Ethernal
Ethernal•14mo ago
Oh, ok, so im extra dumb, lol. So remove the @1 from the from the build command?
James
James•14mo ago
Yeah if you remove the @1, it should build with the prerelease version instead
Want results from more Discord servers?
Add your server