auth_basic 모듈을 Nginx 에서도 사용할수 있습니다.
간략 사용법
location / {
auth_basic “인증 메세지”;
auth_basic_user_file /path/to/directory/user-password-file;
}
아파치에서 사용하는 htpasswd 바이너리 파일을 이용해 사용자와 비밀번호가 담긴 파일을 생성합니다.
(-c 옵션은 새로운 파일을 생성한다는 뜻이며 -m 옵션은 비밀번호를 MD5 로 암호화 합니다. )
예제1
[root@yongbok ~]# /usr/local/apache2/bin/htpasswd -cm /home/ruo91/html/auth/user-pass
ruo91
New password:
Re-type new password:
Adding password for user ruo91
[root@yongbok ~]# cat /home/ruo91/html/auth/user-pass
ruo91:$apr1$TOQWFAC6$jlvLKdTpXRZMNoSerzEzw1
아파치의 htpasswd 가 없을 경우 python으로 생성시켜 줄수도 있습니다. (htpasswd.py)
옵션 및 사용법
-c : 파일생성
-b : 파일이름
[root@yongbok ~]# ./htpasswd.py -c -b [password file] [user name] [user password]
예제2
[root@yongbok ~]# wget http://mirror.yongbok.net/ruo91/nginx/script/htpasswd.py
[root@yongbok ~]# chmod +x htpasswd.py
[root@yongbok ~]# ./htpasswd.py -c -b /home/ruo91/html/auth/user-pass ruo91 1234
[root@yongbok ~]# cat /home/ruo91/html/auth/user-pass
ruo91:VQgWb2rauYGk2
Nginx에서 실제로 적용 해보면 아래와 같습니다.
예제3
server {
listen 80;
server_name
ruo91.yongbok.net;root /home/ruo91/html;
index index.php index.html;access_log log/ruo91/ruo91-access.log;
error_log log/ruo91/ruo91-error.log;
location ~ ^/auth {
auth_basic “Admin Auth [ID : ruo91 Pass : 1234]”;
auth_basic_user_file /home/ruo91/html/auth/user-pass;
}location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /home/ruo91/html$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi.conf;
}
}
Nginx 재시작 (Master pid 를 알아내고 죽이면 됩니다.)
[root@yongbok ~]# ps -x | grep -v grep | grep nginx | awk ‘{ print $1 }’
43608
[root@yongbok ~]# kill -HUP 43608