TrustedProxy does not detect User IP

为君一笑 提交于 2020-03-21 06:09:26

问题


I'm using Laravel 5.3 with PHP7 on an EC2 behind an ELB and Cloudflare and I have a little problem with TrustedProxy package(https://github.com/fideloper/TrustedProxy).

The package doesn't give me the user Ip.

In the trusted config file I have something like this

return [
    'proxies' => '*',
    'headers' => [
        \Illuminate\Http\Request::HEADER_CLIENT_IP    => 'X_FORWARDED_FOR',
        \Illuminate\Http\Request::HEADER_CLIENT_HOST  => 'X_FORWARDED_HOST',
        \Illuminate\Http\Request::HEADER_CLIENT_PROTO => 'X_FORWARDED_PROTO',
        \Illuminate\Http\Request::HEADER_CLIENT_PORT  => 'X_FORWARDED_PORT',
    ],
];

(I tried with ** too)

But when I do something like $request->ip(), $request->getClientIp o getClientIps (at the end all this method are using getClientIps()) the IP that appear is the proxy IP.

If I print the $request I can see:

Server bag:

...
"HTTP_X_FORWARDED_PROTO" => "https"
"HTTP_X_FORWARDED_PORT" => "443"
"HTTP_X_FORWARDED_FOR" => "XX.XX.XX.XX" (I removed the real IP)
...
"REMOTE_ADDR" => "10.0.0.53"

Header bag:

"x-forwarded-proto" => array:1 [
    0 => "https"
  ]
  "x-forwarded-port" => array:1 [
    0 => "443"
  ]
  "x-forwarded-for" => array:1 [
    0 => "XX.XX.XX.XX" (I removed the real IP)
  ]

But the $request->ip(), $request->getClientIp and getClientIps return the wrong IP (10.0.0.53).

Does anyone have an idea or clue ?

Thanks!


回答1:


Solved. The main problem (and a bit stupid) is the order in the middelwares. I were using the information in a middleware executed before trustproxy.



来源:https://stackoverflow.com/questions/41251519/trustedproxy-does-not-detect-user-ip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!