3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ロードバランサ構成でもCakePHPのredirectを使いたい

Last updated at Posted at 2020-06-07

バージョン

CakePHP:3.8

構成

ブラウザ
   ---(HTTPS)--- Load-Balancer(wish SSLサーバ証明書)
   ---(HTTP)--- WEBサーバ

やりたいこと・困りごと

  1. Cake\Controller\Controller::redirect的なのを使いたい
  2. Load-Balancer経由のアクセスでは、httpsでredirectして欲しい
  3. WEBサーバIPアドレスへのアクセスでは、httpでredirectして欲しい

解決法1:beforeFilterで書き換え

src/Controller/AppController.php

function beforeFilter(Event $event) {
    ・・・
    if ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
        Router::fullBaseUrl( 'https://'.$_SERVER['HTTP_HOST'] );
    }
    ・・・
}
  • AWSのElastic Load Balancingは、HTTP_X_FORWARDED_PROTOやHTTP_X_FORWARDED_PORTで判定できる
  • 他のロードバランサの場合、どのヘッダで判断できるか調査必要

解決法2:Cake\Controller\Controller::redirectをoverwrite

src/Controller/AppController.php

public function redirect($url, $status = 302) {
    $this->response = $this->response->withLocation(Router::url($url, false));
    return parent::redirect($url, $status);
}
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?