LoginSignup
2
0

More than 5 years have passed since last update.

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

Posted at

前提

  • 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";
}
2
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
2
0