1. Apache HTTP 서버의 이해

1.1 개요

  • Apache HTTP 서버는 아파치 소프트웨어 재단(ASF:Apache Software Foundation)에서 개발하여 배포하고 있는 무료/오픈소스 웹 서버이다.
  • 아파치 HTTP 서버는 전세계 웹 서버 시장 점유율의 50% 이상을 차지하고 있으며 , 리눅스, 유닉스, BSD, 윈도우즈 등 다양한 플랫폼에서 사용이 가능하다.
  • 아파치 HTTP 서버는 빠르고 효율적이며, 이식성이 좋고 안정적이며, 기능이 다양하고 확장성이 좋다.

1.2 Apache HTTP 서버 왜 필요한가?

  • 보안기능 (SSL, Proxy, ACL, Directory 접근제한등..)
  • 성능적인 측면(리소스 분산처리, Cache(Expires), HTTP 표준설정(ETag등), MPM(Multi-Processing Module), KeepAlive등..)
  • 가상호스트 기능(하나의 서버에 여러 도메인 운영, 서버호스팅등..)
  • 운영적인 측면 (ErrorDocument, Access Log등..)
  • 부가적인 기능 (여러가지 유용한 Apache Module등..)

1.3 설치

2. Apache HTTP 서버의 주요 설정

2.1 가상호스트 (VirtualHost)

  • VirtualHost(가상호스트)란 하나의 웹 서버에서 여러 개의 도메인 주소를 운영 및 관리 할 수 있는 기능이다.
  • Apache HTTP Server에서는 이름기반 가상호스트와 IP 기반 가상호스트 기능을 제공한다.
이름기반 가상호스트(Name-based VirtualHost) 설정
  • 하나의 IP 주소에 여러 개의 호스 도메인 주소 사용이 가능하다.
  • httpd-vhost.conf의 VirtualHost를 설정하여 이름기반의 가상 호스트를 사용 할 수 있다.
  • httpd.conf 파일에서 httpd-vhosts.conf 파일의 주석을 해제해야 한다.

Name-based VirtualHost 예제


Listen 80
NameVirtualHost *:80

<VirtualHost *:80>
  ServerName www.gurubee.net
  ServerAlias www.gurubee.net
  DocumentRoot C:\workspace\project\oracleclub
</VirtualHost>

<VirtualHost *:80>
  ServerName wiki.gurubee.net
  DocumentRoot C:\workspace\project\wiki
</VirtualHost>

가상호스트 설정 실습
  • $APACHE_HOME/conf/extra/httpd-vhost.conf 파일을 열어 NameVirtualHost와 VirtualHost를 아래와 같이 설정한다.
  • ServerName은 test.apache.org로 입력하고, DocumentRoot는 아파치 $APACHE_HOME/htdocs로 설정한다.

httpd-vhost.conf



NameVirtualHost *:80

<VirtualHost *:80>
    ServerName test.apache.org
    DocumentRoot C:\dev\Apache2.2\htdocs    # Apache htdocs 디렉토리 
</VirtualHost> 

  • 아래와 같이 C:\Windows\System32\drivers\etc\hosts 파일에 호스트 설정을 추가 한다.
  • ^hosts.zip 파일을 받아 바로가기를 만들어 사용하면 편하다.

C:WindowsSystem32driversetchosts


# 127.0.0.1  localhost
127.0.0.1  test.apache.org

2.2 Files, Directory, Location 섹션

Files (파일시스템 관점)
  • 특정 파일에 대한 접근제한을 설정한다.
  • 아래는 위치에 상관없이 private.html 파일에 대한 접근을 제한하는 예이다.
 
<Files private.html>
Order allow,deny
Deny from all
</Files>

  • 아래는 "/home/user/webapps" 경로아래에 있는 private.html 파일에 대한 접근을 제한하는 예이다.
 
<Directory /home/user/webapps>
<Files private.html>
Order allow,deny
Deny from all
</Files>
</Directory>

Directory (파일시스템 관점)
  • 운영체제 입장에서 디스크를 보는 관점이다. 운영체제 디렉토리에 대한 접근제한을 설정한다.
  • 아래 예제를 보면서 Allow,Deny에 대해 이해를 해보자
    • Order 절 순서대로 Allow와 Deny를 실행한다
    • 아래 예는 Allow를 먼저 수행하고, Deny를 수행한다.
    • 즉 모두 허용하고, 127.0.0, 192.168.123 두 개의 아이피 대역에 대해서 접근 제한을 설정한다 .
 
# 특정 IP 대역의 IP 차단
<Directory /usr/local/apache/htdocs>
    Order Allow,Deny
    Deny from 127.0.0 192.168.123
    Allow from all
</Directory>

  • 위 예제를 이해하였다면, 아래 예제는 쉽게 이해할 수 있을 것이다.
    • Deny를 먼저 수행하고 Allow를 수행한다.
    • 127.0.0, 192.168.123 두 개의 아이피 대역만 접근이 가능할 것이다.
 
# 특정 IP 대역의 IP 허용
<Directory /usr/local/apache/htdocs>
    Order Deny,Allow
    Allow from 127.0.0 192.168.123
    Deny from all
</Directory>

  • 아래는 Allow, Deny 설정을 잘못한 예제이다.
    • 어떻게 되겠는가?
    • 먼저 Allow를 하고, Deny from all을 하기 때문에 모두 차단하겠다라는 의미를 가진다.
 
<Directory /usr/local/apache/htdocs>
   Order Allow,Deny
   Deny from all
   Allow from 192.168.123.1
</Directory>

Location (웹 경로 관점)
  • 웹서버가 제공하는 경로를 클라이언트가 보는 사이트의 관점이다
  • 아래는 /private 경로에 대해서 접근을 제한하는 예이다.
    • www.gurubee.net/private 문자열로 시작하는 요청이 해당된다.

<Location /private>
Order Allow,Deny
Deny from all
</Location>
 
FilesMatch, DirectoryMatch, LocationMatch
  • 정규표현식을 사용할 수 있다.
  • 아래는 이미지 파일에 대한 접근을 제한하는 예이다.
 
<FilesMatch \.(?i:gif|jpe?g|png)$>
Order allow,deny
Deny from all
</FilesMatch>
 
  • 아래는 WEB-INF, META-INF 디렉토리에 접근을 금지하는 예이다.

<DirectoryMatch "(^|/)META-INF($|/)">
  Order deny,allow
  deny from all
</DirectoryMatch>

<DirectoryMatch "(^|/)WEB-INF($|/)">
  Order deny,allow
  deny from all
</DirectoryMatch>
 
Directory 설정 실습
  • 위의 Directory Allow, Deny 설정 예제를 직접 실습해 보자
  • 아래와 같이 특정 IP의 접근을 막는 Directory 옵션을 추가해 보자 (아래는 로컬 IP 주소의 접근을 막는 예제이다.)
  • Apache restart 후 http://test.apache.org 접속시 "Forbidden" 에러 메시지가 나오는지 확인 해 보자

httpd-vhost.conf


<VirtualHost *:80>
    ServerName test.apache.org
    DocumentRoot C:\dev\Apache2.2\htdocs    
    
    <Directory C:\dev\Apache2.2\htdocs>
        Order Allow,Deny
        Deny from 192.168  127.0.0
        Allow from all
    </Directory>    
</VirtualHost>

2.3 ErrorDocument

ErrorDocument란
  • Apache HTTP Server에서는 ErrorDocument 지시자를 사용해 특정 에러발생시 특정 페이지로 redirect 할 수 있다.
  • ErrorDocumen를 활용하여 다양한 HTTP Status Code에 대해서 설정을 해놓으면 사용자에게 좀 더 편리하고 친절하게 메시지를 보여 줄 수 있다.
  • httpd.conf 파일에서 ErrorDocument 지시자를 설정하면 된다.
  • 아래와 같이 세가지 방법으로 설정 할 수 있다.

ErrorDocument 설정 예제


# 1. plain text 설정 방법
# 500 메세지를 "The server made a boo boo." 로 변경해 준다.
ErrorDocument 500 "The server made a boo boo."

# 2. local redirects 방법
# 404 발생시 missing.html의 내용을 보여준다.
ErrorDocument 404 /missing.html

# 3. external redirects 설정 방법
# 404 발생시 외부 페이지로 redirect 한다.
ErrorDocument 404 http://www.example.com/subscription_info.html

ErrorDocument 설정 실습
  • Directory 에서 실습한 접근권한 설정예제에서 403 접근 권한 오류가 발생 했을 때 Apache HTTP Server의 기본 메시지인 "Forbidden" 에러 메시지 대신 특정 HTML 이 나오게 실습 해보자
  • ^AccessDeny.html 파일을 "$APACHE_HOME/htdocs" 디렉토리에 저장한다.
  • 아래와 같이 ErrorDocument를 설정 한다.

httpd-vhost.conf


<VirtualHost *:80>
    ServerName test.apache.org
    DocumentRoot C:\dev\Apache2.2\htdocs    

    ErrorDocument 403 /AccessDeny.html   
    
    <Directory C:\dev\Apache2.2\htdocs>
        Order Allow,Deny
        Deny from 192.168.123 127.0.0
        Allow from all
    </Directory>    
</VirtualHost>

IE ErrorDocument 설정 기준
  • error page 의 사이즈가 아래 기준보다 작으면 IE 메세지를 보여준다.(브라우저가 알아서 에러 페이지가 성의 없다고 판단함)
CodeDescriptionFile Size
400Bad Request512 bytes
403Forbidden256 bytes
404Not Found512 bytes
500Internal Server Error512 bytes

참고자료