https 下分页生成的链接 http 解决方法
首先我们获取一下当前的网络连接是不是HTTPS
use \Illuminate\Http\Request;
.....
$request->getScheme();
......
跟踪代码我们可以看到 $this->isSecure()
/*
*
* Gets the request's scheme.
*
* @return string
*/
public function getScheme()
{
return $this->isSecure() ? 'https' : 'http';
}
继续跟踪代码 在isSecure()
中我们可以得到自己的答案
/*
*
*/
public function isSecure()
{
//首先去检查访问客户端ip是否在白名单里 可以通过setTrustedProxies()进行设置
// 这里有个坑,isFromTrustedProxy()验证的是客户端ip,只能在局域网中使用,如果你需要局域网外使用就不适用
if ($this->isFromTrustedProxy()
&& $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
}
// 取巧的方式就是 我们可以手动去设置变量HTTPS $this->server->set('HTTPS', 'on');
$https = $this->server->get('HTTPS');
return !empty($https) && 'off' !== strtolower($https);
}
从结果来看,我这边使用负载均衡无法通过$this->isFromTrustedProxy()
就意味着我们需要自己去设置全局属性
在AppServiceProvider
中
public function boot()
{
if ($this->app->environment('production')) {
app(Request::class)->server->set('HTTPS', 'on');
}
}
这里只是提供我的解决方法,可能使用有不恰当。
如果有更好的方法,请留言补充
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: