【问题标题】:Get real Visitors IP in Apache behind Nginx reverse proxy dont work在 Nginx 反向代理后面的 Apache 中获取真正的访客 IP 不起作用
【发布时间】:2020-05-13 00:56:54
【问题描述】:

在尝试解决问题 5 小时并阅读我找到的任何教程后,我在这里寻求帮助。

我有一个指向我的Apache Web 服务器的Nginx Reverse Proxy 服务器,但在Apache 上,我只从Nginx reverse proxy 服务器获得了IP address,而不是访问者IP。

我的 nginx 代理文件: /etc/nginx/sites-available/proxy.conf

proxy_pass http://MYIPADDRESS;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header        Host            $host;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header        X-Real-IP       $remote_addr;

在 Apache 上我已添加到 /etc/apache2/apache2.conf 文件:

RemoteIPHeader X-Forwarded-For

替换

LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{X-Forwarded-For}i" combined

a2enmod remoteip 也已启用

在 Nginx /var/log/nginx/access.log 文件中,我总是得到localhost IP 之类的:

127.0.0.1 - - [27/Jan/2020:23:53:07 +0800] "GET...

也曾尝试添加到/etc/nginx/nginx.conf

set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For

如果我现在在我的网络服务器上运行浏览器中的 PHP 脚本:

<?php

echo 'User IP - '.$_SERVER['REMOTE_ADDR'];

?>

我刚刚获得了反向代理服务器 IP,并且它与整个服务器相同。

两个服务器操作系统都是 Debian 9,有人知道吗?非常感谢!

【问题讨论】:

    标签: apache nginx proxy reverse-proxy


    【解决方案1】:

    首先,确保您安装并启用了mod_remoteip apache 模块。

    我在我的服务器上使用以下配置。

    在 httpd.conf 中:

    LoadModule remoteip_module modules/mod_remoteip.so
    
    RemoteIPHeader X-Forwarded-For
    RemoteIPInternalProxy 127.0.0.1
    

    在nginx站点配置文件中:

    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $http_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto $scheme;
    

    【讨论】:

    • 这一行是什么意思? proxy_set_header X-Forwarded-Proto $scheme; 如果我使用你的代码,我的直接 IP 访问被阻止错误
    • 这会将 X-Forwarded-Prodo HTTP 标头设置为“http”或“https”,具体取决于访问者协议。
    • 好吧,如果我只使用您的 httpd.conf 内容并用我的代理 IP 替换 127.0.0.1 它作为用户 IP 127.0.0.1,我认为它是正确的,但 nginx 不记录用户 IP,因为在我的/var/log/nginx/access.log 它也适用于每个访问者 127.0.0.1 作为 IP 正确吗?如果是,我该如何解决?
    • 也许ngx_http_realip_module?
    • 不,ngx_http_realip_module 除了一些其他(可能还有 nginx)反向代理或负载平衡服务器之外,还需要使用 nginx。
    猜你喜欢
    • 2021-09-01
    • 2020-10-21
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多