【发布时间】:2016-12-16 09:18:32
【问题描述】:
我已将 nginx 配置为反向代理。但是,ghost 总是从 nginx 转发给它的请求中获取相同的 ip 127.0.0.1。
如何让ghost从nginx获取真实IP?
我的nginx配置包括以下语句
proxy_set_header Host $http_host;
proxy_set_header X-real-ip $remote_addr;
【问题讨论】:
我已将 nginx 配置为反向代理。但是,ghost 总是从 nginx 转发给它的请求中获取相同的 ip 127.0.0.1。
如何让ghost从nginx获取真实IP?
我的nginx配置包括以下语句
proxy_set_header Host $http_host;
proxy_set_header X-real-ip $remote_addr;
【问题讨论】:
您应该先尝试从标头中检索 IP 地址:
var ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
不建议直接覆盖req.connection.remoteAddress,因为它会混淆与您合作的其他程序员。但这在技术上是可行的。 remoteAddress 是一个getter,所以你不能直接给它赋值,你需要define your own getter:
req.connection.__defineGetter__('remoteAddress', function() {
return req.headers['x-real-ip'];
});
【讨论】: