3

Symfony 不信任我的 nginx 反向代理:我在日志中看不到用户的真实 IP,所有内容都使用 IP 127.0.0.1 记录

我在 app.php 中的配置:

Request::setTrustedProxies(
    ['127.0.0.1', $request->getClientIp(), $request->server->get('REMOTE_ADDR')],
    Request::HEADER_X_FORWARDED_ALL
);

我的 nginx 反向代理配置:

location / {
    proxy_pass http://localhost:8042;
    proxy_set_header        Host             $host;
    proxy_set_header        X-Real-IP        $remote_addr;
    proxy_set_header        X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header        X-Forwarded-Host $remote_addr;
}

这是 symfony 请求的 var_dump :

  ["server"]=>
  object(Symfony\Component\HttpFoundation\ServerBag)#9 (1) {
    ["parameters":protected]=>
      ["HTTP_X_FORWARDED_FOR"]=>
      string(14) "176.158.246.32"
      ["HTTP_X_REAL_IP"]=>
      string(14) "176.158.246.32"
      ["SERVER_PORT"]=>
      string(4) "8042"
      ["SERVER_ADDR"]=>
      string(9) "127.0.0.1"
      ["REMOTE_PORT"]=>
      string(5) "49538"
      ["REMOTE_ADDR"]=>
      string(9) "127.0.0.1"
      ["DOCUMENT_URI"]=>
      string(8) "/app.php"
      ["REQUEST_URI"]=>
      string(14) "/debug-request"
      ["SCRIPT_NAME"]=>
      string(8) "/app.php"
      ["PHP_SELF"]=>
      string(8) "/app.php"
    }
  }
  ["headers"]=>
  object(Symfony\Component\HttpFoundation\HeaderBag)#10 (2) {
    ["headers":protected]=>
    array(13) {
      ["x-forwarded-for"]=>
      array(1) {
        [0]=>
        string(14) "176.158.246.32"
      }
      ["x-real-ip"]=>
      array(1) {
        [0]=>
        string(14) "176.158.246.32"
      }
    }
  }
  ["isHostValid":"Symfony\Component\HttpFoundation\Request":private]=>
  bool(true)
  ["isClientIpsValid":"Symfony\Component\HttpFoundation\Request":private]=>
  bool(true)
  ["isForwardedValid":"Symfony\Component\HttpFoundation\Request":private]=>
  bool(true)
}
4

0 回答 0