request() 在 Laravel 中使用负载均衡器和 TrustedProxies 不安全

request() not secure in Laravel with load balancer and TrustedProxies

为什么 request()->isSecure() 在访问 HTTPS-url 时返回 false?我正在使用 Laravel Forge 并且我有一个负载平衡器。记录请求我得到这个数据:

request()->url(),          // "http://xx.xx"
request()->isSecure(),     // false
request()->getClientIps(), // XX.XX.XX.XX 

In TrustProxies(添加到Http/Kernel.php中的$middleware数组):

protected $proxies = [
    'XX.XX.XX.XX', // exactly the same as in the logged data above
];

在我的AppServiceProviderboot()方法中:

if (env('APP_ENV') == 'production') {
    \URL::forceScheme('https');
}

编辑:

我也在使用 Cloudflare,我也已将所有 Cloudflare 代理添加到 $proxies。检查日志,我在请求中看到这些 headers:

X-Forwarded-Proto: http
Cf-Visitor:        {"scheme":"https"}
Referer:           https://xx.xx/someurl

当您使用 Cloudflare 时,您应该使用“完全 SSL/TLS 加密模式”。