【问题标题】:How to make drupal see client IP behind nginx Reverse Proxy->Varnish->Apache stack?如何让drupal看到nginx反向代理->清漆->Apache堆栈后面的客户端IP?
【发布时间】:2020-02-18 19:31:02
【问题描述】:

我使用 nginx 作为反向代理来处理端口 80 -> 443 重定向,然后到与服务于 drupal (7.69) 站点的 Apache2.4 后端(端口 8182)对话的清漆缓存(端口 8181)。

我已将 nginx 配置为将客户端 IP 转发为:

proxy_pass            http://127.0.0.1:8181;
proxy_read_timeout    90;
proxy_connect_timeout 90;
proxy_redirect        off;
proxy_set_header      X-Real-IP $remote_addr;
proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header      X-Forwarded-Proto https;
proxy_set_header      X-Forwarded-Port 443;
proxy_set_header      X-Forwarded-Host $remote_addr;
proxy_set_header      Host $host;

我在清漆 vcl_recv 中添加:

if (req.restarts == 0) {
  if (req.http.X-Forwarded-For) {
    set req.http.X-Forwarded-For =
    req.http.X-Forwarded-For + ", " + client.ip;
  } else {
    set req.http.X-Forwarded-For = client.ip;
  }
}

在 apache 中我已经安装了 remoteip 模块并设置了以下配置:

RemoteIPHeader X-Forwarded-For
RemoteIPTrustedProxy 127.0.0.1

在drupal的settings.php中我有

$conf['reverse_proxy'] = True;
$conf['reverse_proxy_header'] = 'X-Forwarded-For';
$conf['reverse_proxy_addresses'] = array('127.0.0.1');

...但是,drupal 似乎仍然只看到和登录 localhost (127.0.0.1) ip,而不是真正的客户端 IP。因此,表单被记录为不是来自真正的用户,而是来自服务器本身。

我想知道我的 varnish 配置是否已关闭,因为我还从 nginx 代理并绕过 varnish 直接进入绕过 drupal(它是 SMF)但在相同的 apache 配置上运行的论坛;论坛正在记录从 nginx 发送的客户端 IP(使用相同的标头更改和 apache remoteip 配置)就好了。

非常感谢您的指导/建议。

【问题讨论】:

    标签: drupal-7 varnish nginx-reverse-proxy apache2.4


    【解决方案1】:

    清漆

    理论上,您对 X-Forwarded-For 的 Varnish 配置是正确的。

    实际上,您甚至不需要在 Varnish 中设置 X-Forwarded-For,因为 Varnish 会自动执行此操作。

    Drupal 配置

    就 Drupal 配置而言,它可能与 reverse_proxy_header 有关。我偶然发现了这段 Drupal 7 文档:https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/ip_address/7.x

    该页面上代码的关键部分如下:

    $reverse_proxy_header = variable_get('reverse_proxy_header', 'HTTP_X_FORWARDED_FOR');

    显然这个ip_address() 函数可以通过配置参数知道X-Forwarded-For 标头。

    但是,header 格式不是官方的 HTTP 格式,而是 PHP $_SERVER superglobal 存储它的方式。在这种情况下,格式为HTTP_X_FORWARDED_FOR

    所以请尝试将$conf['reverse_proxy_header'] = 'X-Forwarded-For'; 替换为$conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR';

    【讨论】:

    • 辉煌;作品!从那以后,我几乎实现了这一点,并在 Varnish 中创建了自己的变量来编码 IP 地址,其前缀与我需要的相同。出于某种原因,我没有先尝试简单的解决方案。非常感谢您为我指明了正确的方向。
    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2021-03-02
    • 2010-12-03
    相关资源
    最近更新 更多