0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FilamentのFileUploadでMixed Contentエラー!? → `trustProxies` で解決した話

Posted at

Filamentでファイルアップロード機能を使っていたときに、
JavaScriptがHTTPで読み込まれてMixed Contentエラーになる問題に遭遇しました。

結論から言うと、bootstrap/app.phptrustProxies(at: '*') を追加することで解決しました💡
備忘録として残しておきます。


🔥 起きた問題

  • Laravel 10 + Filament を使用
  • FileUpload コンポーネントを使ったら、以下のような現象が発生:

ブラウザのコンソールに以下のエラーが出力:
Mixed Content: The page at 'https://example.com/admin/orders/create' was loaded over HTTPS, but requested an insecure script 'http://example.com/livewire/upload-file.js'

このせいで、ファイルアップロードが正常に動作しない…


🧙‍♂️ 解決方法:trustProxies を追加するだけ!

Laravel 10以降の構成で bootstrap/app.php を開き、
以下のように trustProxies(at: '*') を追記します。

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        // 👇 ここが重要
        $middleware->trustProxies(at: '*');
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

🧩 補足:なぜこれで直るの?

Laravelはリバースプロキシ(例:Nginx, Cloudflare)経由のリクエストを受けたとき、
実際のプロトコル(HTTP or HTTPS)を正確に判別できないことがあります。

trustProxies() を使って信頼するプロキシを明示することで、
request()->secure() が正しく true を返すようになります。

これにより、Livewireが生成するJavaScriptのURLも https:// になります。

✅ 結果:ちゃんと動いた!

この設定を追加したら、
FilamentのFileUploadコンポーネントがHTTPSでも問題なく動作するようになりました🎉
JSの読み込みも https:// になってMixed Contentは完全解消!

📝 まとめ

Filamentでファイルアップロードがうまくいかないときは「Mixed Content」を疑うべし

Laravel 10+ の bootstrap/app.php に trustProxies(at: '*') を書くと一発解決するかも!

特に Cloudflare や Nginx の裏で動かしてる場合は要注意!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?