Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

489
Visualizações
Obtain the client's IP in Nextjs

I need the client's IP in Next. But unfortunately, all the methods I searched for, gave me the IP of the server and the local IP of the server. As far as I know, the requests from the client first go to the server and after passing Nginx, it is redirected to the site, where the IP is obtained, which is the IP of the server. Here are all the methods I have used so far.

//in _app.js file
1)appContext?.ctx?.req?.headers["x-forwarded-for"] || "")
      .split(",")
      .pop()
      .trim() || 2)appContext?.ctx?.req?.connection?.remoteAddress;

3)appContext?.ctx?.req?.socket?.remoteAddress;

4)requestIp.getClientIp(appContext?.ctx?.req);

5)let ip =
    appContext?.ctx?.req?.headers["x-real-ip"] ||
    appContext?.ctx?.req?.connection.remoteAddress;
  if (ip.substr(0, 7) == "::ffff:") {
    ip = ip.substr(7);
  }
  
6)appContext?.ctx?.req?.headers["x-userip"];

7)appContext?.ctx?.req?.clientIp

8)const forwarded = appContext?.ctx?.req?.headers["x-forwarded-for"];
  const ip = forwarded
    ? forwarded.split(/, /)[0]
    : appContext?.ctx?.req?.connection?.remoteAddress;
//in server.js file
9)server.set("trust proxy", false);

10)server.set("trust proxy", true);

import requestIp from "request-ip";
11)server.use(requestIp.mw());

12)server.use(function (req, res, next) {
    if (req.method === "get") {
      var ip = req.headers["x-real-ip"] || req.connection.remoteAddress;
      if (ip.substr(0, 7) == "::ffff:") {
        ip = ip.substr(7);
        console.log("ippppppppp", ip);
      }
    }
    next();
  });
  
13)server.use(function (req, res) {
    const ip = req.clientIp;
    res.end(ip);
  });
  

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda