Laravel 包含混合内容中的 css 文件

Sar*_*hal -1 assets laravel mixed-content

我正在尝试在负载均衡器后面的服务器上发布 Laravel 网站。域 SSL 托管在负载均衡器上以强制执行 HTTPS。但是,托管该网站的服务器没有 SSL。这会导致请求资产时 HTTPS 和 HTTP 不匹配。

在服务器上时,该网站可以完美运行。(localhost/CentralizedSettings/login) 在服务器外部请求时( https://blahSite.com/CentralizedSettings/login ),css 文件被涂黑,出现此错误:

错误信息:

Mixed Content: The page at 'https://blahSite.com/CentralizedSettings/login' was loaded over HTTPS, 
but requested an insecure stylesheet 'http://blahSite.com/CentralizedSettings/css/app.css'. 
This request has been blocked; the content must be served over HTTPS.
Run Code Online (Sandbox Code Playgroud)

head.blade.php

<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

.env 文件:

APP_ENV=local
APP_URL=https://blahSite.com/CentralizedSettings
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

- Adding the APP_URL to the .env file
- Changing the url to localhost
- Using asset(mix('css/app.css'))
Run Code Online (Sandbox Code Playgroud)

Lk7*_*k77 5

我认为解决方案是在生产中强制使用 https:

<?php

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\URL;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        if($this->app->environment('production')) {
            URL::forceScheme('https');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是使用 ASSET_URL :

.env

ASSET_URL=https://example.com
Run Code Online (Sandbox Code Playgroud)

.env.local

ASSET_URL=http://local.example.com
Run Code Online (Sandbox Code Playgroud)