Figuring out if the current Request
is secure or not should not be your decision. Underlying Symfony\Component\HttpFoundation\Request
has isSecure
method that Laravel uses internally.
public function isSecure()
{
if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
return \in_array(strtolower($proto[0]), array('https', 'on', 'ssl', '1'), true);
}
$https = $this->server->get('HTTPS');
return !empty($https) && 'off' !== strtolower($https);
}
So if your server is not passing the HTTPS
header with On
, it should be passing X-FORWARDED-PROTO
and must be allowed by your TrustProxies
middleware.
If you are behind reverse-proxy you should find out your proxy pass IP - you can do this easily by getting the $_SERVER['REMOTE_ADDR']
variable and setting the IP to your TrustProxies
middleware:
/**
* The trusted proxies for this application.
*
* @var array
*/
protected $proxies = [
'123.123.123.123',
];
Laravel (Symfony) will then automatically detect if the Request
is secure or not and choose the protocol accordingly.