在从 Apache 调用的 PHP-FPM 中设置正确的 REMOTE_ADDR

Mar*_*náš 6 php apache ip fastcgi nginx

我们希望在 Apache 服务器上从 mod_php 切换到 fastCGI + PHP-FPM。

我们一切就绪并正在工作,除了一件事:

$_SERVER['REMOTE_ADDR'] 中的值始终是 127.0.0.1,而不是客户端的 IP。有什么方法可以配置服务器将此变量设置为客户端真实IP吗?

我们在 X-Forwarded-For 标头中有客户端真实 IP(从代理传递)

基本上我们需要 Apache 替代 nginx 配置:

fastcgi_param REMOTE_ADDR $http_x_forwarded_for;
Run Code Online (Sandbox Code Playgroud)

(如此处所述,Nginx 将 REMOTE_ADDR 替换为 X-Forwarded-For

小智 5

如果你想在日志中查看真实IP,请使用%{REMOTE_ADDR}inaccess.format指令。在php-fpm.conf你的泳池里。

  • 正确的格式是 `%{REMOTE_ADDR}e` 而不是 `%{REMOTE_ADDR}` (2认同)

Tan*_*Tat 0

在 nginx 配置中设置它。

# Set the client remote address to the one sent in the X_FORWARDED_FOR header from trusted addresses.
set_real_ip_from                127.0.0.1;
real_ip_header                  X-Forwarded-For;
real_ip_recursive               on;
Run Code Online (Sandbox Code Playgroud)

来源:http://nginx.org/en/docs/http/ngx_http_realip_module.html

--

Apache 更新

对于 Apache,安装 modulemod_rpaf并配置它。

LoadModule              rpaf_module modules/mod_rpaf.so
RPAF_Enable             On
RPAF_ProxyIPs           127.0.0.1 10.0.0.0/24
RPAF_SetHostName        On
RPAF_SetHTTPS           On
RPAF_SetPort            On
RPAF_ForbidIfNotProxy   Off
Run Code Online (Sandbox Code Playgroud)

来源: https: //github.com/gnif/mod_rpaf