getScheme still has a protocol issue
I have read through https://github.com/laravel/framework/issues/2171 and related code. However it has an issue if the code is working behind SSL termination point. Fore example if request hits SSL termination point and then balanced to non-SSL server Laravel would think it is non SSL request.
Request over HTTPS ==> Balancer/SSL Termination point ==> HTTP Servers (Oops Laravel thinks it is HTTP and makes http:// like links --> it's blocked on client side)
I think it's better to have protocolless URLs. They work fine for both HTTP and HTTPS. So it would look like <script src="//cdnjs.cloudflare.com/ajax/libs/...
What about allowing http/https to be set in a configuration file?
@berle Do you mean 'HTTPS Only Cookies'?
As in, having a setting in app/config/app.php (for instance) that would let you force Laravel to generate urls with the https scheme.
secure_urls => true,
It would still lead to mixed content if the original request is non-SSL. I think servers behind balancer can't know if the original request was in HTTPS. Thus neutral links is more universal so IMO while it's not fixed replace 'http://' and 'https://' with '//' in output.
Cloudflare, as an example of an SSL terminator, acts as a reverse proxy to your backend servers. I imagine this is also how other SSL terminators and load balancers work. When the request is forwarded from Cloudflare to your backend server, they provide various X-Forwarded-* headers, with X-Forwarded-Proto specifying whether the original request was for http or https.
As Laravel uses the Symfony Request class, we can leverage the trusted proxies feature.
Firstly, set up the Request class using the Request::setTrustedProxies() method to let Laravel know which upstream load balancers or SSL terminators to trust X-Forwarded headers from (see example). From then on, calls to e.g. Request::getClientIp() or Request::isSecure() will act on the original request to the load balancer, not the request from the load balancer to you, which should solve the problem of SSL terminating upstream.
Thanks @maknz That's a good workaround. It would be nice if Laravel could make URLs in 'auto' (as it works now) and 'neutral' modes. Sending proxy headers looks overcomplicated. For those who has similar problem add the following proxy directives in nginx config
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Proxy-Port $proxy_port; # <-- I'm not sure if this one correct as it sends 80 in my case
proxy_set_header X-Forwarded-Proto $scheme;
and the following (cloudflare IPs in the example below) somewhere in Laravel (I used start.php)
Request::setTrustedProxies(array(
'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',
));
If you don't want to put more stuff in start.php, there is a package for this: https://github.com/fideloper/TrustedProxy.
@GrahamCampbell Thanks but that's really one line of code
Request::setTrustedProxies(($trustedProxies = Config::get('app.trustedProxies')) === '*' ? [Request::getClientIp()] : $trustedProxies );