Unable to get client IP or request protocol in development mode
Closed this issue · 9 comments
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: npm@8.19.3
- 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
Are you checking log in development mode or when deploying? (if deployed, what preset)
For dev, see unjs/nitro#1978 it is fixed in nightly channels you can try nuxt nightly.
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' ]
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
?
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?)
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.
Current workaround:
- Switch to nightly channel (for unjs/nitro@29ddd94)
- Use
nuxi dev --no-frok
fix should have been landed in latest nitro, try upgrading with npx nuxi upgrade --force