javascript - 如何从请求nodejs中获取ip
问题描述
我想从发布请求中获取 ip。
var ip = req.ip || req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
(req.connection.socket ? req.connection.socket.remoteAddress : null)
res.json({
success: true,
message: 'ip'+ip
});
但它返回一个null
值。我在服务上使用(它不是本地主机)
有什么解决办法吗?所有解决方案都返回null
;
解决方案
如果它位于像 Nginx 这样的反向代理之后,请尝试使用来自 ngx_http_core_module 的嵌入变量。(至少这就是我现在多年来所做的事情)。
Nginx 配置文件
....
location / {
proxy_set_header X-Real-IP $remote_addr;
....
节点.js
req.headers["x-real-ip"]
推荐阅读
- c++ - Bellman Ford 将 Source & Destination 解析为字符而不是整数
- python - 如何通过 API 使用 For Loop 从 Google Drive 下载文件
- java - 使用 Apache Spark 和 Java 按列分组并将每组字符串写入文本文件
- mariadb - 将系统版本化(临时)MariaDB 数据库从 Windows 移动到 Linux
- reactjs - 如何为我的 React CMS 应用程序创建添加文件夹功能?
- java - 无法在 rest api 中返回媒体类型“application/xml”的对象列表
- vue.js - 如何从 store.js 调用组件方法
- java - cplex - 矩阵值和索引的最小化和总和
- node.js - 无法从回调向用户返回交易响应
- html - 使用 HTML 和 CSS 构建表格