Use req.headers.get("x-forwarded-for") or req.headers.get("x-real-ip") to get the IP address in Next.js Middleware (Ensure nginx is properly configured to forward the IP-related headers).