'Get client IP in getServerSideProps method in NextJs [duplicate]

I am trying to find the IP of the client in getServerSideProps in NextJs. I simply use basic IP found methods but when I call that method in getServerSideProps which always giving me the server IP, not Client IP.



Solution 1:[1]

You can do it like this:

export const getServerSideProps = async ({ req }) => {
  const forwarded = req.headers['x-forwarded-for'];

  const ip = typeof forwarded === 'string' ? forwarded.split(/, /)[0] : req.socket.remoteAddress;

  console.log(ip);

  return {
    props: { ip },
  };
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Diogo Capela