LoginSignup
36
36

More than 5 years have passed since last update.

Amazon,S3にてBasic認証をかける

Last updated at Posted at 2015-01-22

S3で静的サイトをホスティングする、Static Website Hosting。
便利で高性能だが、Basic認証がかからない。

リバースプロキシを介することで対応してみる。
リバースプロキシサーバがボトルネックとなり、せっかくのStatic Website Hostingの性能も生かせないが、とりあえずやってみる。

描くまでもないが、構成はこんな感じ。

Untitled (3).png

以下、手順。
① webサーバでリバースプロキシの設定を行う。

httpd.conf
<IfModule mod_proxy.c>
ProxyPass /hoge http://xxx.s3-website-ap-northeast-1.amazonaws.com/app
ProxyPassReverse /hoge http://xxx.s3-website-ap-northeast-1.amazonaws.com/app
</IfModule>

② webサーバでリバースプロキシにBasic認証を設定

httpd.conf
<IfModule mod_proxy.c>
  <Proxy *> 
    AllowOverride FileInfo AuthConfig Limit 
    Options MultiViews Indexes SymLinksIfOwnerMatch 

    AuthType Basic
    AuthName "Secret Zone"
    AuthUserFile /xxx/yyy/.htpasswd
    Require user fuga
  </Proxy> 
...
</IfModule>

③ S3のバケットポリシーで、送信元IPアドレスをwebサーバに絞る。

{
    "Version": "2008-10-17",
    "Id": "Policy1403165896111",
    "Statement": [
        {
            "Sid": "Stmt1403165893575",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::xxx/*",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "##.##.##.##/##"
                    ]
                }
            }
        }
    ]
}
36
36
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
36
36