How can I obtain client's ip address in Next.js 13 middleware?
Unanswered
Chilean jack mackerel posted this in #help-forum
Chilean jack mackerelOP
Like in title I tried to get IP from request headers
req.headers.get('x-forwarded-for') but it's not there. Any ideas?18 Replies
Chilean jack mackerelOP
I'm using
13.4.19 nextjs version but I remember that on my older project with version 13.4.12 It was working fine.this type of question is really frequently asked but it's a bit tricky as it depends on where to host. where did u host?
Chilean jack mackerelOP
well I'm hosting on vercel but now I'm testing it in local dev mode. So the
x-forwarded-for header should return the ::1 value (like in 13.4.12 version) but it's not.@joulev in middleware you have req.ip
Chilean jack mackerelOP
it's also undefined
does it work on vercel?
x-forward-for is layer 7. you mean ip layer address?
@joulev does it work on vercel?
Chilean jack mackerelOP
I will check
@Chilean jack mackerel it's also undefined
req.ip ?? "127.0.0.1" is a practice i've seen people usingChilean jack mackerelOP
well yeah but I remember that
x-forwarded-for header was defined too@joulev does it work on vercel?
Chilean jack mackerelOP
yeah it works on vercel
so now the x-forwarded-for is not there in local server
afaik both req.ip and x-forwarded-for doesn't work very reliably (i.e. you likely get correct value from it but that is not guaranteed)
so you just have to specify a fallback in case the value is not present or is invalid
Barbary Lion
hey there, did you find any solution on this one?
i've updated to latest and got the same exactly issue, "x-forwarded-for" is not included anymore on the headers array
while it used to be in the previous version i had