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

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

More than 5 years have passed since last update.


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


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




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


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

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

  # CloudFront 経由での下記のアクセスは禁止
  location /admin {
    return 403;
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
Help us understand the problem. What are the problem?