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
You could try using middleware or next.config.js rewrites perhaps
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/*
Are you using
@cloudflare/next-on-pages
?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 thatAh... That's a bug then. Sorry about that - I'll look into a fix
Oh, alright. thank you
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
Ok, I'll go ahead and try that. thank you!
Hm, it's still redirecting me
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.
I followed in installation guide on the PR, and my package.json reports
and then I pushed it to git and cloudflare redeployed the site
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 thatAh- I had missed that part in the documentation, My build command is still
next build
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 🙂Oh, ok, so im extra dumb, lol. So remove the @1 from the from the build command?
Yeah if you remove the
@1
, it should build with the prerelease version insteadGot 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.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
you can't fetch IPs from Workers/Functions, only domains
ex. You could set up a proxied subdomain and fetch that from your function
so setup my API on
api.domain
and then proxy /api
-> api.domain
Yeah give that a go