Nginx AWS auth on Ubuntu 10.04

S3に保存されたファイルを直接S3にアクセスさせたくない場合のNginxの設定です。

通常は、素直にクラウドフロントを利用すればいいような気もしますが、調べてみたのでメモしておきます。

Nginxから認証付きでS3にアクセスするにはサードパーティのモジュールAWS Authを利用します。
モジュールは以下のリンクからダウンロードできます。

http://wiki.nginx.org/3rdPartyModulesJa

インストール

モジュールを追加してNginxをコンパイルするために必要な
unzipとgccを含むbuild-essentialをあらかじめインストールしておきます。

$ sudo apt-get update

2012.11.03現在では、apt-get updateしておかないとリポジトリのパスが変わっていてgccのインストールに失敗します

$ sudo apt-get install unzip gcc build-essential
$ sudo apt-get install libpcre3 libpcre3-dev
$ sudo apt-get install zlib1g-dev
$ sudo apt-get install openssl libssl-dev

Nginxのソースをダウンロードして解凍しておきます。

$ wget http://nginx.org/download/nginx-1.2.4.tar.gz
$ tar xvfz nginx-1.2.4.tar.gz 
$ cd nginx-1.2.4

次にAWS Authモジュールをダウンロードします。

$ wget https://github.com/anomalizer/ngx_aws_auth/zipball/master
$ mv master aws-auth.zip
$ unzip aws-auth.zip

configureしてmake

$./configure --sbin-path=/usr/sbin \
        --conf-path=/etc/nginx/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/lock/nginx.lock \
        --http-log-path=/var/log/nginx/access.log \
        --http-client-body-temp-path=/var/lib/nginx/body \
        --http-proxy-temp-path=/var/lib/nginx/proxy \
        --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
        --with-debug \
        --with-http_stub_status_module \
        --with-http_flv_module \
        --with-http_ssl_module \
        --with-http_dav_module \
        --with-http_gzip_static_module \
        --with-http_realip_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-ipv6 \
        --add-module=./anomalizer-ngx_aws_auth-37adfc3

...
configuring additional modules
adding module in ./anomalizer-ngx_aws_auth-37adfc3/
 + ngx_http_aws_auth was configured
checking for PCRE library ... found
checking for PCRE JIT support ... not found
checking for system md library ... not found
checking for system md5 library ... not found
checking for OpenSSL md5 crypto library ... not found
checking for sha1 in system md library ... not found
checking for OpenSSL sha1 crypto library ... not found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/sbin"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/body"
  nginx http proxy temporary files: "/var/lib/nginx/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/fastcgi"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

$ make
...
	objs/src/http/modules/ngx_http_browser_module.o \
	objs/src/http/modules/ngx_http_upstream_ip_hash_module.o \
	objs/src/http/modules/ngx_http_upstream_least_conn_module.o \
	objs/src/http/modules/ngx_http_upstream_keepalive_module.o \
	objs/addon/anomalizer-ngx_aws_auth-37adfc3/ngx_http_aws_auth.o \
	objs/ngx_modules.o \
	-lpthread -lcrypt -lssl -lpcre -lcrypto -lcrypto -lz
make[1]: Leaving directory `/home/hrendoh/nginx-1.2.2'
make -f objs/Makefile manpage
make[1]: Entering directory `/home/hrendoh/nginx-1.2.2'
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
		-e "s|%%PID_PATH%%|/var/run/nginx.pid|" \
		-e "s|%%CONF_PATH%%|/etc/nginx/nginx.conf|" \
		-e "s|%%ERROR_LOG_PATH%%|/var/log/nginx/error.log|" \
		< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/home/hrendoh/nginx-1.2.2'
...

インストール

$ sudo make install

設定

こんな感じです

location /s/attachment/ {
  proxy_pass http://<バケット名>.s3.amazonaws.com/;
  aws_access_key xxxxxxxxxxxxxxxx;
  aws_secret_key xxxxxxxxxxxxxxxx
  s3_bucket <バケット名>;
  chop_prefix /s/attachment;
		
  proxy_set_header Authorization $s3_auth_token;
  proxy_set_header x-amz-date $aws_date;
}