node.js - How to Get the Client IP Address in a Next.js Application Hosted on Vercel? - Stack Overflow

I have a Next.js application hosted on Vercel at example. I also have an API hosted at api.example, whi

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:

  1. 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:

  1. 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.
Share Improve this question edited Nov 20, 2024 at 22:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 20, 2024 at 16:29 hantorenhantoren 1,2656 gold badges27 silver badges47 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Are 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信