javascript - 由于反向代理,无法在生产中获取客户端的 IP
问题描述
我的 nodejs 后端应用程序在开发中运行良好。但现在我意识到,由于在开发中我有一个从前端到 /api 的所有请求的反向代理,所有req.ip
值现在都是 localhost。
如何在不使用第三方 api 的情况下解决它?看起来我需要在客户端的 ip 到达后端之前获取它?
我正在使用 nginx 作为反向代理。
解决方案
as per Benard's suggestion, I had to add the following line to the express server file:
server.set("trust proxy", true);
then, in the nginx config:
# node api reverse proxy
location /api/ {
proxy_set_header X-Forwarded-For $remote_addr; // added this
proxy_pass http://localhost:5000/;
}
restart the nginx and the express server, and req.ip will work.
推荐阅读
- r - Windows 上 R 中并行计算的输出
- java - 通过 ModelMapper 映射时将 LocalDateTime.now() 的值设置为两个字段
- r - 如何将数据框随机拆分为在主题和项目上平衡的两半
- dns - 无法 ping 桉树云中的 DNS 服务器
- python - 用双轴在两点之间画线
- html - HTML 中的正文内容
- javascript - 使用 AudioContext API Safari 在 HTML5 音频元素中阻止跨源?
- typo3 - 缓存问题:后端和前端的不同域 Typo3
- nginx - NGINX:基于查询字符串的别名或代理
- swift - 自定义单元格未选择不调用委托