Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

前提

  • 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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした