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•17mo ago
You could try using middleware or next.config.js rewrites perhaps
Ethernal
EthernalOP•17mo 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•17mo ago
Are you using @cloudflare/next-on-pages?
Ethernal
EthernalOP•17mo 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•17mo ago
Ah... That's a bug then. Sorry about that - I'll look into a fix
Ethernal
EthernalOP•17mo ago
Oh, alright. thank you
James
James•17mo 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
EthernalOP•17mo ago
Ok, I'll go ahead and try that. thank you! Hm, it's still redirecting me
James
James•17mo 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
EthernalOP•17mo 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•17mo 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
EthernalOP•17mo ago
Ah- I had missed that part in the documentation, My build command is still next build FacePalm
James
James•17mo 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
EthernalOP•17mo ago
Oh, ok, so im extra dumb, lol. So remove the @1 from the from the build command?
James
James•17mo ago
Yeah if you remove the @1, it should build with the prerelease version instead
Ethernal
EthernalOP•17mo ago
Got it, removed the @1 and reverted to next build. redeploying now Kind of works, but now im getting a 1003, Direct IP access not allowed.
James
James•17mo ago
That'll be unrelated to anything in next-on-pages, and is instead because Cloudflare doesn't allow direct ip access to IPs in its network
Chaika
Chaika•17mo ago
you can't fetch IPs from Workers/Functions, only domains ex. You could set up a proxied subdomain and fetch that from your function
Ethernal
EthernalOP•17mo ago
so setup my API on api.domain and then proxy /api -> api.domain
James
James•17mo ago
Yeah give that a go
Want results from more Discord servers?
Add your server