DEV Community

Yasuhiro Matsuda for AWS Community Builders

Posted on

CloudFront経由のアクセスでプロトコルを識別する

主にWordpress向けのネタになると思うが、ALB経由でのアクセスでプロトコルを識別する場合には、ターゲットに対してX-Forwarded-Proto リクエストヘッダーを使用してアクセスされるため、HTTP_X_FORWARDED_PROTOの環境変数で識別できる一方、CloudFront経由のアクセスでプロトコルを識別するする場合には、CloudFront-Forwarded-Protoヘッダをオリジンアクセス時に付加することで、HTTP_CLOUDFRONT_FORWARDED_PROTOの環境変数で識別できる。

しかしながら、CloudFrontのオリジンリクエストポリシー名がマネージドポリシーを使っている場合、AllViewerではCloudFront-Forwarded-Protoヘッダがオリジンに送られることはなく、HTTP_CLOUDFRONT_FORWARDED_PROTOの環境変数で識別することができない。

Managed-AllViewerAndCloudFrontHeaders-2022-06というマネージドポリシーが用意されているので、これを設定することで要件を満たすようになる。

CloudFront のリクエストヘッダーの追加には以下の記述が冒頭あるが、どのように設定できるのかが同一ページに記載されていないので、紹介してみた。

CloudFront がビューワーから受け取り、オリジンまたはエッジ関数に転送するリクエストに特定の HTTP ヘッダーを追加するように CloudFront を設定できます。

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay