Skip to content

Unable to get client IP or request protocol in development mode #324

Closed
@xtance

Description

@xtance

Environment

[2:31:36 PM] Nuxt project info:


  • Operating System: Windows_NT
  • Node Version: v18.13.0
  • Nuxt Version: 3.9.1
  • CLI Version: 3.10.0
  • Nitro Version: 2.8.1
  • Package Manager: [email protected]
  • Builder: -
  • User Config: devtools
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Nuxt project with a middleware and a plugin, both failing to get a client IP:
https://github.com/xtance/nuxt-app-ip/

As compared to the blank h3 app:
https://github.com/xtance/h3-app-ip

Describe the bug

I'm trying to get client IP address from an event and it couldn't show it. The only way is to read x-forwarded-for header and there it's always 127.0.0.1

export default defineNuxtRouteMiddleware((to, from) => {
    const nuxt = useNuxtApp();
    const event = useRequestEvent(nuxt);
    console.log(
        'IP',
        event.context.clientAddress,
        event.node.req.connection.remoteAddress,
        event.node.req.socket.remoteAddress,
        getRequestIP(event),
        getRequestIP(event, { xForwardedFor: true }),
    );
})

It returns "IP undefined undefined undefined undefined 127.0.0.1" (I also tried with a server plugin in /plugins/server)

Since I read that nuxt uses h3 I created a blank project to test it and it shows my real IP. (See in reproduction section)

Additional context

I was wondering if I'm alone with this issue but here is a discussion: nuxt/nuxt#25059

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions