I have a Next.js application hosted on Vercel at example. I also have an API hosted at api.example, which receives requests from the frontend. However, when I try to log the client IP address in the API, I only see the IP address of example (the Vercel server) instead of the actual client IP address.
Here’s what I’ve tried so far:
- Reading req.headers['x-forwarded-for']: I added middleware on my API to log this header, but it always seems to contain the Vercel server IP.
I have a Next.js application hosted on Vercel at example. I also have an API hosted at api.example, which receives requests from the frontend. However, when I try to log the client IP address in the API, I only see the IP address of example (the Vercel server) instead of the actual client IP address.
Here’s what I’ve tried so far:
- Reading req.headers['x-forwarded-for']: I added middleware on my API to log this header, but it always seems to contain the Vercel server IP.
1 Answer
Reset to default 1Are you reading the first IP of the returned array, which should be the client one?
const clientIp = (req.headers['x-forwarded-for'] || '').split(',')[0].trim()
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742344452a4426266.html
评论列表(0条)