【问题标题】:How does ghost get real IP from nginx(reverse proxy)?ghost如何从nginx(反向代理)获取真实IP?
【发布时间】: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


    【解决方案1】:

    您应该先尝试从标头中检索 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'];
    });
    

    【讨论】:

    • 谢谢。现在我想修改ghost的源代码,让它在所有地方都使用真实IP。我可以在第一次生成请求对象的地方直接用 req.headers['x-real-ip'] 的值对 req.connection.remoteAddress 进行签名吗?这样req.connection.remoteAddress就有了真实IP的值,我只需要修改源码中的一处即可。 :)
    猜你喜欢
    • 2020-05-31
    • 1970-01-01
    • 2020-10-21
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2012-06-22
    相关资源
    最近更新 更多