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?

【AWS】CloudFront + S3におけるインデックスドキュメントの挙動の違いと設定パターンまとめ

0
Posted at

はじめに

CloudFront (以下CF) とS3を組み合わせて静的ウェブサイトを公開する際、
http://example.com/http://example.com/about のようなパスで index.html を自動的に表示させるには、いくつかの設定が必要です。

特に以下の点で挙動が異なるため、用途に応じた選択が重要です。

  • CFのDefault Root Objectの設定
  • S3の静的ウェブサイトホスティング設定
  • OAC (Origin Access Control) の有無

本記事ではそれぞれのパターンとその違いについて解説します。

結論:どちらの構成にするかで挙動が大きく変わる

パターン① サブディレクトリ単位 (example.com/about) にアクセスしたときに index.html を表示させたい場合

  • S3の静的ウェブサイトホスティングを有効にし、インデックスドキュメントを設定する必要があります。
    これにてexample.comexample.com/about にアクセスした場合にindex.htmlが返されます。
  • CFのOACは利用できません(OACは通常のオブジェクトアクセスに使い、ウェブサイトホスティングには対応していません)。

パターン② OACを有効にし、セキュアな構成で公開したい場合

  • CloudFront側でデフォルトルートオブジェクトindex.html を指定します。
    これにてexample.com にアクセスした場合にindex.htmlが返されます。
  • ただし、example.com/about にアクセスしても ブロックされます。
    アクセスするにはファイル名まで含めた example.com/about/index.html を直接指定する必要があります。
  • このとき、S3側のインデックスドキュメント設定は影響しません

各構成の挙動比較

設定項目 パターン①:
サブディレクトリに対しても
index.html を返す
パターン②:
ルートパス (/) のみに
index.html を返す
S3静的ウェブサイトホスティング 必要 不要
S3ブロックパブリックアクセス (バケット設定) 無効にする必要あり 有効のままでOK
CloudFront OACの使用 不可 可能
CloudFront Default Root Object 不要 必須 (index.html など)

代替案:CloudFront Function / Lambda@Edge の活用

/about というパスで about/index.html を読み込ませたいが、OACを使いたいという場合は、CloudFront Functions や Lambda@Edge を使ってURLの書き換え(リダイレクト)を行う方法もあります。

ただし、これらの機能はリクエスト数に応じた追加料金が発生するため、アクセス数が多いサイトではコストに注意が必要です。

まとめ

要件 構成
example.com/aboutindex.html を表示したい S3静的ウェブサイトホスティングを有効にする構成(OAC不可)
セキュアに OAC を使いたい CloudFront の Default Root Object 設定を使用(パス指定に制約あり)

用途やセキュリティ要件、コストを踏まえ、最適な構成を選択してください。

参考資料


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?