【问题标题】:Laravel asset URL ignoring httpsLaravel 资产 URL 忽略 https
【发布时间】:2016-03-11 07:29:45
【问题描述】:

我在我的模板中使用以下代码来加载我的 CSS 文件:

<link rel="stylesheet" href="{!! URL::asset('css/app.css') !!}">

如果我在本地计算机上通过 https 查看页面,则 app.css 文件的链接也是 https,但是在我的实时服务器上,这不会发生。如果您通过 https 使用 view the live site 并查看源代码,您会看到指向 CSS 文件的链接仍然超过正常的 http,因此无法加载。有谁知道为什么会这样?

【问题讨论】:

    标签: php laravel https laravel-5 laravel-5.1


    【解决方案1】:

    您可能需要改用secure_asset 函数。

    <link rel="stylesheet" href="{!! secure_asset('css/app.css') !!}">
    

    【讨论】:

    【解决方案2】:

    当您的用户通过 HTTPS 访问时,您的 CloudFlare 配置正在使用 HTTP 将这些请求传递到您的服务器。你有两个选择:

    1. 开启 CloudFlare 的完整或严格 SSL。 https://blog.cloudflare.com/origin-server-connection-security-with-universal-ssl/

    2. 设置您的网络服务器以查看 X-Forwarded-Proto 标头,并在该标头的值为 https 时告诉 PHP 正在通过 HTTPS 访问它。如何做到这一点取决于您的网络服务器。例如,在 nginx 中,我为 Amazon 的 ELB(发送相同的标头)执行此操作:

    map $http_x_forwarded_proto $is_https {
        default off;
        https on;
    }
    

    然后在我的 FastCGI 参数文件中:

    fastcgi_param  HTTPS              $is_https if_not_empty;
    

    在 Apache 中,如果安装了 mod_setenvif,我认为你可以这样做:

    SetEnvIf x-forwarded-proto https HTTPS=on
    

    【讨论】: