LoginSignup
11
14

More than 5 years have passed since last update.

laravelでリンクがhttpsにならない

Posted at

APP_URLで判断してくれると思っていましたが違いました。

asset(絶対パスではない場合)やrouteUrlGeneratorformatSchemeを使用してリンクのスキーマを作り出しています。

laravel\framework\src\Illuminate\Routing\UrlGenerator.php

public function formatScheme($secure)
{
    if (! is_null($secure)) {
        return $secure ? 'https://' : 'http://';
    }

    if (is_null($this->cachedSchema)) {
        $this->cachedSchema = $this->forceScheme ?: $this->request->getScheme().'://';
    }

    return $this->cachedSchema;
}
laravel\framework\src\Illuminate\Routing\UrlGenerator.php
public function forceScheme($schema)
{
    $this->cachedSchema = null;

    $this->forceScheme = $schema.'://';
}
laravel\framework\src\Illuminate\Routing\UrlGenerator.php
public function getScheme()
{
    return $this->isSecure() ? 'https' : 'http';
}

$this->forceSchemeforceSchemeメソッドで設定してしまえば強制的に書き換えることは可能
$this->forceSchemeが設定されていない場合はgetScheme(isSecure)で判定する。
下記のような場合はisSecureで判定できません。

信用するプロキシの設定5.5
信用するプロキシの設定5.6

TLS/SSL証明を行うロードバランサの裏でアプリケーションが実行されている場合、アプリケーションが時々HTTPSリンクを生成しないことに、気づくでしょう。典型的な理由は、トラフィックがロードバランサにより80番ポートへフォワーディングされるため、セキュアなリンクを生成すべきだと判断できないからです。

対応手順

プロキシの問題ではないか確認して、それの問題であればプロキシの設定を追加し、
それでも解決できない場合はforceSchemeメソッドでhttpsを設定してしまう。

if (env('APP_ENV') === 'production')
{
    URL::forceScheme('https');
}
11
14
1

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
11
14