Recommanded Free YOUTUBE Lecture: <% selectedImage[1] %>

Contents

소개

웹 서버를 설정할 때 종종 액세스 자체를 제한해야 하는 경우가 있다. 물론 많은 웹 애플리케이션들이 독자적인 인증/권한 관리 시스템을 제공한다. 하지만 그러한 애플리케이션을 이용 할 수 없거나 "그냥 간단하게 웹 서버가 제공하는 기능을 이용해서" 접근제어를 하고 싶을 때가 있다. 회사 내부에서 사용하는 관리자 페이지가 이런 경우다.

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
파일 내용을 보자.
# cat /etc/nginx/.htpasswd 
yundream:$apr1$0FSBesUn$FxUfWR2uuyhAnwMkCZIwu/
another_user:$apr1$VE6BWoqO$difYn/A5FnmHTQTMFlfgU/

Nginx Password Authentication 설정

Nginx의 기본 설정 파일은 "/etc/nginx/sites-enabled/default" 다. 대략 아래와 같이 설정돼 있을 것이다.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
    }
}
이제 인증을 설정할 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"를 반환한다.
$ curl -i http://localhost/admin
HTTP/1.1 401 Unauthorized
Server: nginx
Date: Mon, 15 Jan 2018 16:24:26 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="Admin page"

<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
HTTP Authentication을 이용해서 URL에 접근하기 위해서는 "http://username:password@example.com" 과 같이 주소 앞에 아이디:패스워드를 설정하면 된다. curl로 테스트해보자.
# curl -v http://yundream:mypassword@localhost/admin/index.html
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 80 (#0)
* Server auth using Basic with user 'yundream'
> GET /admin/index.html HTTP/1.1
> Host: localhost
> Authorization: Basic eXVuZHJlYW06Z2t3bGFr
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 15 Jan 2018 16:28:25 GMT
< Content-Type: text/html
< Content-Length: 12
< Last-Modified: Mon, 15 Jan 2018 16:27:53 GMT
< Connection: keep-alive
< ETag: "5a5cd689-c"
< Accept-Ranges: bytes
< 
Hello World
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact