You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel+JScroll分页因混合内容被拦截问题求助(Cloudflare环境)

问题描述

站点基于Laravel构建,前端使用JScroll实现滚动加载分页。服务器已配置自动HTTPS重定向,通过IP访问HTTPS站点时分页功能正常,但绑定域名后分页失效——AJAX请求的分页URL为HTTP协议,被浏览器因混合内容问题拦截。

站点使用Cloudflare作为CDN及DNS解析器,已开启HTTPS及自动HTTPS重定向,问题仍存在。

控制台错误信息:

Mixed Content: The page at 'https://example.com/posts/all' was loaded over HTTPS, 
but requested an insecure XMLHttpRequest endpoint 'http://example.com/posts/all?page=3'. 
@ jquery.min.js:4  
This request has been blocked; the content must be served over HTTPS. 
send    @   jquery.min.js:4 
ajax    @   jquery.min.js:4 
n.fn.load   @   jquery.min.js:4 
(anonymous) @   jquery.jscroll.min.js:1 
d.complete  @   jquery.min.js:3 
i   @   jquery.min.js:2 
add @   jquery.min.js:2 
_a  @   jquery.min.js:3 
g   @   jquery.min.js:3 
dequeue @   jquery.min.js:3 
(anonymous) @   jquery.min.js:3 
each    @   jquery.min.js:2

滚动分页实现代码:

<script type="text/javascript">
    $('ul.pagination').hide();
    $(function() {
        $('.scrolling-pagination').jscroll({
            autoTrigger: true,
            padding: 20,
            nextSelector: '.pagination li.active + li a',
            contentSelector: 'div.scrolling-pagination',
            callback: function() {
                $('ul.pagination').remove();
            }
        });
    });
</script>

【更新】已定位问题:AJAX请求返回的渲染HTML中,分页URL为HTTP而非HTTPS。
AJAX返回HTML的代码:

$html = view('ajaxData', ['data' => $data, 'filtersArray' => $filtersArray])->render(); 
echo $html;

Blade模板中生成分页的代码:

{{ $data->appends($filtersArray)->links() }}
解决方案

1. 配置Laravel强制HTTPS

Laravel默认根据请求头判断协议,通过Cloudflare时真实协议可能被隐藏,需强制指定HTTPS:

  • 在.env文件中设置:
    APP_URL=https://example.com
    FORCE_HTTPS=true
    
  • 在app/Providers/AppServiceProvider.php的boot方法中添加逻辑:
    public function boot()
    {
        if (env('FORCE_HTTPS', false)) {
            \URL::forceScheme('https');
        }
    }
    

2. 识别Cloudflare转发头

Cloudflare会发送X-Forwarded-Proto标记真实协议,让Laravel识别该头:
在app/Providers/AppServiceProvider.php的boot方法中添加:

public function boot()
{
    if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
        \URL::forceScheme('https');
    }
}

生产环境建议在config/trustedproxy.php中指定Cloudflare的IP段,避免信任所有代理:

'proxies' => ['CloudflareIP1', 'CloudflareIP2'],
'headers' => Illuminate\Http\Request::HEADER_X_FORWARDED_ALL,

3. 分页生成时强制指定HTTPS

若全局配置不生效,可直接在Blade模板中为分页链接指定HTTPS协议:

{{ $data->appends($filtersArray)->setScheme('https')->links() }}

4. 检查服务器重定向配置

确保服务器(Nginx/Apache)传递正确的协议头给Laravel,比如Nginx配置添加:

proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $remote_addr;

内容的提问来源于stack exchange,提问作者Jessica Shamoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:55:50