작은숲:위키노트/아파치 mod deflate 모듈
웹 서버에서 트래픽을 줄이기 위한 가장 쉬운 방법은 압축 전송을 하는 것이다. 요즘은 많은 웹 애플리케이션들이 웹 서버의 압축 전송 기능을 이용하거나 자체적으로 압축해서 전송하는 경우가 많다. 아파치에서는 요청에 대한 응답을 mod_deflate 모듈을 통해 gzip 압축해 전송할 수 있다. mod_deflate 모듈은 기본 모듈이기 때문에 따로 설치할 필요는 없다. 다만 설정에 따라 모듈이 적재되지 않았을 수도 있으니 아파치에 적재된 모듈을 확인한다.
# /usr/local/apache/bin/httpd -D DUMP_MODULES
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_event_module (static)
authn_file_module (shared)
authn_core_module (shared)
authz_host_module (shared)
authz_groupfile_module (shared)
authz_user_module (shared)
authz_core_module (shared)
access_compat_module (shared)
auth_basic_module (shared)
cache_module (shared)
cache_disk_module (shared)
socache_shmcb_module (shared)
reqtimeout_module (shared)
ext_filter_module (shared)
filter_module (shared)
deflate_module (shared)
mime_module (shared)
log_config_module (shared)
env_module (shared)
expires_module (shared)
headers_module (shared)
setenvif_module (shared)
version_module (shared)
ssl_module (shared)
unixd_module (shared)
status_module (shared)
autoindex_module (shared)
vhost_alias_module (shared)
negotiation_module (shared)
dir_module (shared)
alias_module (shared)
rewrite_module (shared)
php5_module (shared)
cband_module (shared)
perl_module (shared)
적재된 모듈 목록 중에 deflate_module (shared)가 있는 것을 확인할 수 있다. 만약 이 항목이 없다면 httpd.conf를 열어 아래 내용을 찾아 주석을 해제하도록 한다.
LoadModule deflate_module modules/mod_deflate.so
httpd.conf를 수정한 후에는 아파치를 재기동하고 적재된 모듈을 확인해서 mod_deflate 모듈이 제대로 올라왔는지 확인한다.
# /etc/init.d/apachectl graceful
이제 httpd.conf나 httpd-vhost.conf 등의 설정 파일에서 mod_deflate 모듈에 대해 설정할 수 있다. 서버에서 서비스하는 모든 웹사이트에 대해 mod_deflate 모듈의 설정을 적용하고자 한다면 httpd.conf에 아래 설정을 넣으면 될 것이고, 그렇지 않다면 각각의 가상 호스트 설정에 추가해도 된다. 만약 .htaccess를 사용할 수 있다면 거기에 넣어도 된다. 물론 httpd.conf나 httpd-vhost.conf 파일에 추가했다면 아파치를 재기동해야 적용된다.
<IfModule deflate_module>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
CustomLog "logs/deflate_log" deflate
</ifModule>
위 설정은 그림 파일을 제외한 모든 파일을 압축해서 보내도록 하고 있다. 그리고 압축을 한 경우에는 얼마나 압축을 했는지 로그를 남긴다. 만약 파일 종류에 따라 압축할 것인지 아닌지를 결정하려면 아래와 같이 AddOutputFilterByType 지시자를 사용한다.
<IfModule deflate_module>
AddOutputFilterByType DEFLATE text/html text/plain
AddOutputFilterByType DEFLATE text/xml application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
</ifModule>
위 설정은 위에서부터 HTML, XML, XHTML/RSS, CSS, 자바스크립트 파일을 압축해서 전송하도록 하는 것이다. 압축해서 전송할 파일의 MIME 형식을 적어주면 된다.
AddOutputFilterByType 지시자를 사용하기 위해서는 mod_filter 모듈이 필요하다. 혹시 아파치에 적재되지 않았다면 httpd.conf 파일에서 모듈 적재하는 부분을 찾아 주석을 해제하도록 한다.
<IfModule deflate_module>
DeflateCompressionLevel 9
</ifModule>
DeflateCompressionLevel 지시자는 얼마나 압축할 것인가를 나타낸다. 기본값은 Zlib의 기본값인 6이고, 1부터 9까지 설정할 수 있으며 9가 가장 많이 압축한다. 하지만 그만큼 CPU를 많이 사용하기 때문에 적당한 값을 주는 것이 좋다. 그리고 DeflateCompressionLevel 지시자는 아파치 설정 파일과 가상 호스트 설정 안에서만 사용할 수 있다. 더 많은 설정에 대해서는 메뉴얼을 참고하도록 하자.
참고
- http://httpd.apache.org/docs/2.4/mod/mod_deflate.html
- http://www.whatsmyip.org/http-compression-test/