웹 서버를 설정할 때 종종 액세스 자체를 제한해야 하는 경우가 있다. 물론 많은 웹 애플리케이션들이 독자적인 인증/권한 관리 시스템을 제공한다. 하지만 그러한 애플리케이션을 이용 할 수 없거나 "그냥 간단하게 웹 서버가 제공하는 기능을 이용해서" 접근제어를 하고 싶을 때가 있다. 회사 내부에서 사용하는 관리자 페이지가 이런 경우다.
Nginx 웹 서버를 이용해서 아이디/패스워드 기반으로 접근을 제어하는 방법을 살펴보자. 우분투리눅스 17.04를 기준으로 설명한다.
Nginx 설치
테스트를 위해서 NginX를 설치하자.
# apt-get update
# apt-get install nginx
Apache 유틸리티를 이용해서 패스워드 파일 만들기
Apache에서 제공하는 "htpasswd"로 아이디/패스워드 파일을 관리 할 수 있다. 아이디/패스워드를 저장 할 파일의 이름은 /etc/nginx/.htpasswd로 설정했다.
htpasswd -c옵션을 이용하면 파일을 새로 만들 수 있다. .htpasswd 파일을 새로 만들고 yundream아이디를 추가해보자.
# sudo htpasswd -c /etc/nginx/.htpasswd yundream
New password:
Re-type new password:
Adding password for user yundream
이 파일에 새로운 유저를 추가하려면 -c 옵션을 빼고 사용하면 된다. another_user아이디를 추가해보자.
# sudo htpasswd /etc/nginx/.htpasswd another_user
New password:
Re-type new password:
Adding password for user another_user
이제 인증을 설정할 location을 설정하면 된다. /admin 디렉토리에 패스워드 인증을 설정했다.
server {
listen 80 default_server;
server_tokens off;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
}
location /admin {
auth_basic "Admin page";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
서버를 재시작한다.
# service nginx restart
패스워드 인증 화면
웹 브라우저를 이용해서 http://localhost/admin 페이지에 접근하려고 하면 아래와 같이 인증창이 뜬다.
curl 로 테스트 해보자. 패스워드 설정을 안하면 "401 Unauthorized"를 반환한다.
Contents
소개
Nginx 설치
Apache 유틸리티를 이용해서 패스워드 파일 만들기
Nginx Password Authentication 설정
패스워드 인증 화면
Recent Posts
Archive Posts
Tags