Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@pm11op

CloudFront 設定時、オリジンへの直接のアクセスに Basic認証をかける (Nginx で)

前提

  • CloudFront のエンドポイント: something.cloudfront.net
  • オリジンとなる EC2 のドメイン: origin.example.com
    • ELB は設定されてない

こうしたい

CloudFront_Basic_Auth.png

事情

  • EC2 にしか権限がない
  • 本当は ELB とか EC2 のセキュリティグループで対応すべき話な気がする

設定

  • CloudFront 経由でのアクセス時、server_name は something.cloudfront.net になる
  • server_name が something.cloudfront.net の場合、そのままアクセスさせる
    • アクセスさせたくない location も設定可能
  • server_name が origin.example.com の場合、IP + Basic認証
/etc/nginx.conf.d/something.cloudfront.net.conf
server {
  listen 80 default;
  server_name something.cloudfront.net;

  # path for static files
  root /path/to/html;

  # CloudFront 経由での下記のアクセスは禁止
  location /admin {
    return 403;
  }    
}
/etc/nginx.conf.d/origin.example.com.conf
server {
  listen 80;
  server_name origin.example.com;

  # path for static files
  root /path/to/html;

  # Basic 認証 + IP, allow と deny の順番重要
  allow {許可したいIP};
  deny all;
  auth_basic  "Restricted";
  auth_basic_user_file "/path/to/.htpasswd";
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?