本文针对Laravel7.3.0测试通过。
背景:由于使用了反向代理,导致Laravel在获取客户端的IP地址时,错误的获取到了反向代理服务器的IP地址,而非真实客户端IP地址。此问题的解决方案如下:
打开 app/Http/Middleware/TrustProxies.php
把protected $proxies;
改为protected $proxies = '*';
获取IP地址使用Laravel自带的函数:request()->ip();
要注意 Nginx 反向代理的location部分要做如下配置:
location / {
proxy_pass http://qd-aliyun-8002;
proxy_redirect default;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
}