Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the ip address of the client from server side in next.js app?

I want to get the user time zone and location to render a server-side page depending on the location and time zone but I can't get the user IP address from the request or get the localhost IP address (127.0.0.1). so what should I do ?

like image 206
mohamed Avatar asked Sep 07 '25 18:09

mohamed


1 Answers

Was able to detect user IP address with next.js app:

export async function getServerSideProps({ req }) {
  const forwarded = req.headers["x-forwarded-for"]
  const ip = forwarded ? forwarded.split(/, /)[0] : req.connection.remoteAddress
  return {
    props: {
      ip,
    },
  }
}
like image 129
Merlo Avatar answered Sep 09 '25 09:09

Merlo