#rate limiter of api and Nextjs

10 messages · Page 1 of 1 (latest)

golden urchin
#

Hi there, I have SSR page which makes a request to my backend retrieves some data and uses that data for page to render

My backend is self hosted behind nginx as reverse proxy, I’ve played a bit with some rate limiting of my backend with detecting how much requests came from same ip address, but now I have a question.

As far as I understand, because I query my api in getServerSideProps, my backend will receive the ip address of the thingy that is hosting my Frontend, not the ip address of the user.

What do I need to do in order to tell my backend that this request is coming from the user that requested the SSR page ?

analog scaffold
#

Some form of user identifier, like a user or session id stored in a cookie

#

Decode the Cooke in your gSSP function

#

And then send the identifier to your api backend

golden urchin
#

Can’t I just pass the user ip as X-Forwarded-For in headers of the request or this will not work?

analog scaffold
#

You can get the ip address of the user too, it would be in req object that gSSP receives

#

Yep

golden urchin
# analog scaffold You can get the ip address of the user too, it would be in req object that gSSP ...

How would one do that? I found this on stackoverflow, https://stackoverflow.com/questions/68338838/how-to-get-the-ip-address-of-the-client-from-server-side-in-next-js-app

But there are a lot of confusing answers which are then commented as “not working”

#

const forwarded = ctx.req.headers["x-forwarded-for"]
const ip = forwarded ? forwarded.split(/, /)[0] : ctx.req.connection.remoteAddress

This should work, right?

analog scaffold
#

Second answer is how I have it set up if I recall