LoginSignup
10
6

More than 5 years have passed since last update.

CloudFrontの特有ヘッダを一般的なヘッダに書き換えて楽をするメモ

Last updated at Posted at 2016-12-13

WEBアプリケーションの前段に CDN として CloudFront を使っているとスマホ判定やhttps判定でCloudFront用の特別設定を開発者側にしてもらう必要がある。

具体的には User-Agent が取れない代わりに CloudFront-Is-Mobile-Viewer, CloudFront-Is-Tablet-Viewer, CloudFront-Is-Desktop-Viewer, CloudFront-Is-SmartTV-Viewer などを見るようにする。
また、一般敵なProxyを経由したときのhttps判定では X-Forwarded-Proto を見るところ、CloudFront では CloudFront-Forwarded-Proto というヘッダを見てやる必要がある。

で、多くのアプリでは User-Agent 判定に iPhone/iPad などの文字列が含まれているかを使っており、またhttps判定では ${HTTPS} =on または標準的な X-Forwarded-Proto ヘッダ辺りを見ているものがおおい。そしてCloudFrontのヘッダに対応しているものは少ない。
なので、CloudFrontのヘッダを見つけたら、対応されていることが期待できる User-AgentX-Forwarded-Proto ヘッダを擬似ってやることでアプリケーション開発者/デプロイ作業者の作業を少し減らしてやろう。というのが本エントリの趣旨です。

リクエストヘッダを書き換える設定

で、以下のような設定を httpd.conf に書いておいてあげることで、php や .htaccess などのアプリケーション側では普通のWEBサーバにデプロイされたときと同じように User-Agent や X-Forwarded-Proto を見て判定すれば、それらのヘッダについて特別 CloudFront の存在を意識する必要はなくなる。

httpd.conf
# アプリ側でCloudFrontスペシャルのスマホ判定をしなくてよいようヘッダを書き換える
SetEnvIf CloudFront-Is-Mobile-Viewer true is-mobile
SetEnvIf CloudFront-Is-Tablet-Viewer true is-tablet
RequestHeader set User-Agent "Amazon CloudFront (iPhone)" env=is-mobile
RequestHeader set User-Agent "Amazon CloudFront (iPad)" env=is-tablet
# アプリ側でCloudFrontスペシャルのhttps判定をしなくてよいようヘッダを書き換える
SetEnvIf CloudFront-Forwarded-Proto https is-https HTTPS=on
RequestHeader set X-Forwarded-Proto https env=is-https

終わり。

10
6
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
10
6