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);
});