開発ブログ

株式会社Nextatのスタッフがお送りする技術コラムメインのブログ。

電話でのお問合わせ 075-744-6842 ([月]-[金] 10:00〜17:00)

  1. top >
  2. 開発ブログ >
  3. PHP >
  4. Laravel >
  5. 【Laravel】AWSのCloudFront経由で公開する時のドメインプロキシ設定

【Laravel】AWSのCloudFront経由で公開する時のドメインプロキシ設定

こんにちは。
ニシザワです。

LaravelをAWSのCloudFront経由で公開する場合
ドメインがCDN経由になるため変わってしまいます。 そのため、今回はそちらの対処法を書きたいと思います。
Laravel5.5以降は標準でTrusted Proxyが入りました。
ですので設定するだけで大丈夫です。

Cloud Frontの設定

スクリーンショット 2018-11-02 18.01.47.png Cloud FrontのBehaviorのWhitelist HeadersにCloudFront-Forwarded-ProtoHostを設定します。
なお、SP等の判定をしたい場合はUser-agentを追加しましょう。

Laravelの設定

config/trustedproxy.phpを作成し以下を追加します。
<?php

return [

    /*
     * Set trusted proxy IP addresses.
     *
     * Both IPv4 and IPv6 addresses are
     * supported, along with CIDR notation.
     *
     * The "*" character is syntactic sugar
     * within TrustedProxy to trust any proxy
     * that connects directly to your server,
     * a requirement when you cannot know the address
     * of your proxy (e.g. if using Rackspace balancers).
     *
     * The "**" character is syntactic sugar within
     * TrustedProxy to trust not just any proxy that
     * connects directly to your server, but also
     * proxies that connect to those proxies, and all
     * the way back until you reach the original source
     * IP. It will mean that $request->getClientIp()
     * always gets the originating client IP, no matter
     * how many proxies that client's request has
     * subsequently passed through.
     */
    'proxies' => '*',

    /*
     * Default Header Names
     *
     * Change these if the proxy does
     * not send the default header names.
     *
     * Note that headers such as X-Forwarded-For
     * are transformed to HTTP_X_FORWARDED_FOR format.
     *
     * The following are Symfony defaults, found in
     * \Symfony\Component\HttpFoundation\Request::$trustedHeaders
     *
     * You may optionally set headers to 'null' here if you'd like
     * for them to be considered untrusted instead. Ex:
     *
     * Illuminate\Http\Request::HEADER_CLIENT_HOST  => null,
     *
     * WARNING: If you're using AWS Elastic Load Balancing or Heroku,
     * the FORWARDED and X_FORWARDED_HOST headers should be set to null
     * as they are currently unsupported there.
     */
    'headers' => [
        (defined('Illuminate\Http\Request::HEADER_FORWARDED') ? Illuminate\Http\Request::HEADER_FORWARDED : 'forwarded') => 'FORWARDED',
        Illuminate\Http\Request::HEADER_X_FORWARDED_FOR    => 'X_FORWARDED_FOR',
        Illuminate\Http\Request::HEADER_X_FORWARDED_HOST  => 'X_FORWARDED_HOST',
        Illuminate\Http\Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
        Illuminate\Http\Request::HEADER_X_FORWARDED_PORT  => 'CLOUDFRONT_FORWARDED_PROTO'
    ]
];
ポイントはIlluminate\Http\Request::HEADER_X_FORWARDED_PORT => 'CLOUDFRONT_FORWARDED_PROTO'を追加することです。

最後にapp/Http/Middleware/TrustProxies.phpのheaderを空にします。
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array
     */
    protected $proxies;

    /**
     * The current proxy header mappings.
     *
     * @var array
     */
    protected $headers = [
        //ここを空にする。
    ];
}

今回は設定ファイルを優先させるため初期設定は削除します。

以上で、URLがCDN経由で来ても思ったとおりになります。
TOPに戻る