最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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()
发布评论

评论列表(0)

  1. 暂无评论