Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xtance opened this issue Jan 8, 2024 · 9 comments
Closed

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

xtance opened this issue Jan 8, 2024 · 9 comments

Comments

@xtance
Copy link

xtance commented Jan 8, 2024

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

@pi0
Copy link
Member

pi0 commented Jan 8, 2024

Are you checking log in development mode or when deploying? (if deployed, what preset)

For dev, see nitrojs/nitro#1978 it is fixed in nightly channels you can try nuxt nightly.

@xtance
Copy link
Author

xtance commented Jan 8, 2024

I'm testing in development mode, with "dev": "nuxt dev --host 0.0.0.0" to expose on the network, but the IP stays undefined regardless of how I access the Nuxt site (localhost:3000 / 127.0.0.1:3000 / my-ip:3000 )

Thank you for the hint but switching to nightly unfortunately didn't affect this issue ._.

Nuxt 3.9.2-28408245.6736decb wit
h Nitro 2.9.0-28406650.c8e8549

the result of middleware execution is is:

[Nitro/request] [ 'IP', undefined, undefined,
 undefined, undefined, '127.0.0.1' ]

@pi0 pi0 transferred this issue from nuxt/nuxt Jan 8, 2024
@pi0
Copy link
Member

pi0 commented Jan 8, 2024

I suspect this might be bug in nuxt CLI's proxy. Will investigate more.

In the meantime, can you also try nuxt dev --no-fork --host 0.0.0.0?

@xtance
Copy link
Author

xtance commented Jan 8, 2024

I suspect this might be bug in nuxt CLI's proxy. Will investigate more.

In the meantime, can you also try nuxt dev --no-fork --host 0.0.0.0?

Adding --no-fork actually worked: it puts my real IP in x-forwarded-for. Other options are still undefined, but the header already works fine, thank you for your help.

(By the way, is there documentation about this command argument and what does it affect?)

@pi0 pi0 added bug Something isn't working command:dev dev-proxy labels Jan 8, 2024
@pi0
Copy link
Member

pi0 commented Jan 8, 2024

Thanks for confirming. Using getRequestIP is the right way we use several internal proxies (cli, nuxt and nitro) in order to make development experience better but in situation of any reverse proxy in production also you might always need to check the headers instead of raw socket.

@pi0 pi0 changed the title Unable to get client IP Unable to get client IP or request protocol in development mode Jan 10, 2024
@pi0
Copy link
Member

pi0 commented Jan 10, 2024

Current workaround:

@pi0
Copy link
Member

pi0 commented Mar 18, 2024

fix should have been landed in latest nitro, try upgrading with npx nuxi upgrade --force

@pi0 pi0 closed this as completed Mar 18, 2024
@murshex
Copy link
Contributor

murshex commented Sep 26, 2024

@pi0 I’m getting “http” from getRequestProtocol even though my server is running https. nuxi dev --no-fork fixes it.

@pi0
Copy link
Member

pi0 commented Sep 26, 2024

@c0nd3v ~> #487

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants