open-next
open-next copied to clipboard
Middleware's request.ip doesn't provide the client's IP
In Vercel hosted NextJS app in middleware.ts you can get the client's IP from request.ip
(might also be in route.ts file in the app directory but I'm not sure and haven't tested there), but in SST deployed NextJS app there's nothing in the request.ip
.
request.remoteAddress
Both of these no longer exist on NextRequest
We can probably close this. Today you would get the IP address from the request headers. either cloudfront-viewer-address
or x-forwarded-for
As @sommeeeer pointed out there is other ways to get the IP address.
For sure I was just noting it if anyone stumbles here from Google tbh.
Best way to get IP is
req.headers['x-forwarded-for'] || '').split(',').pop().trim()
This always gets the last entry, gets around XFF spoofing for certain environments.