Skip to content

NextRequest ip is undefined, and geo is empty object #37366

@IvonaPilcevic

Description

@IvonaPilcevic

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:22 PDT 2022; root:xnu-8020.121.3~4/RELEASE_X86_64
Binaries:
Node: 14.17.0
npm: 6.14.13
Yarn: 1.22.15
pnpm: N/A
Relevant packages:
next: 12.1.4
react: 18.0.0
react-dom: 18.0.0

warn - Latest canary version not detected, detected: "12.1.4", newest: "12.1.7-canary.27".
Please try the latest canary version (npm install next@canary) to confirm the issue still exists before creating a new issue.
Read more - https://nextjs.org/docs/messages/opening-an-issue

What browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

nginx, docker

Describe the Bug

Hi,
I am using _middleware in my project, and I want to access users ip address.
However, when I try to get it from request.ip it returns undefined.
What is the solution to this problem?

Here is a snippet of my code:

`export async function middleware(request: NextRequest) {
const ip = request.ip -----> returns undefined or null;
const city = request.geo -----> returns {};

const ip2 = request.headers.get('x-forwarded-for');
const ip3 = request.headers.get('CF-IPCountry');
}`

I was desperate, so I tried to get ip through request.headers, but this also doesn't work.
Is there another solution to get users ip address in middleware?

I also tried using service const response = await fetch('https://api.ipify.org/?format=json'); but this only returns server ip address, and not my real ip.

Also, i deployed the app, and ip still doesn't work: https://relock-front-stage.tbstaging.dev/

Please help :D

Expected Behavior

get user ip address after accessing request.ip

To Reproduce

in _middleware.ts try to access request.ip or request.geo

Metadata

Metadata

Assignees

No one assigned

    Labels

    MiddlewareRelated to Next.js Middleware.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions