繁体   English   中英

在 Nginx 反向代理背后的 Apache 中获取真实访客 IP 不起作用

[英]Get real Visitors IP in Apache behind Nginx reverse proxy dont work

在尝试解决问题并阅读我找到的任何教程 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,整个服务器的 IP 都是一样的。

两台服务器操作系统都是 Debian 9,有任何想法吗? 非常感谢!

首先,确保您安装并启用了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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM