Fix SSL in Laravel 4 when using Cloudflare

Submitted by zOxta - 10 years ago

If you are running Laravel 4 behind Cloudflare over HTTPS some functions like ‘Request::secure()’ and ‘Request::isSecure()’ will return false. By using the setTrustedProxies method of the Request facade we can add the Cloudflare IP ranges to trust, thus the Request class will honor the ‘X-Forwarded-Proto’ and other ‘X-Forwarded’ headers.

// in your filters.php add the following inside App::before()
// for an up to date list of CloudFlare IP addresses https://www.cloudflare.com/ips

App::before( function( $request )
{
    // CloudFlare IP addresses to trust
    Request::setTrustedProxies( [
       '199.27.128.0/21',
       '173.245.48.0/20',
       '103.21.244.0/22',
       '103.22.200.0/22',
       '103.31.4.0/22',
       '141.101.64.0/18',
       '108.162.192.0/18',
       '190.93.240.0/20',
       '188.114.96.0/20',
       '197.234.240.0/22',
       '198.41.128.0/17',
       '162.158.0.0/15',
       '104.16.0.0/12',
   ] );
});