end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転します

nginx + amon2 for perl でのbasic認証

nginx設定

「proxy_set_header X-Forwarded-User $remote_user;」を設定します

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    server {
        listen       8080;
        server_name  localhost;

        location / {
            proxy_pass      http://127.0.0.1:5000;

            auth_basic "MEMBER ONLY";
            auth_basic_user_file /home/endo/dev/htpasswd;

            proxy_set_header Host             $host;
            proxy_set_header X-Real-IP        $remote_addr;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-User $remote_user; ## HERE !!
        }
    }
}


amon2 というか、perl アプリケーション

contextオブジェクトから、「HTTP_X_FORWARDED_USER」を取り出します

sub get_login_user {
    my ($self,$c) = @_;
    my $user_id = $c->req->{env}->{HTTP_X_FORWARDED_USER};
    return Splats::Model::User->new($c,$user_id);
}