Computer/LINUX

mod_expire

알찬돌삐 2012. 8. 10. 16:19

Apache에서 이미지 캐싱 처리(mod_expires)

등록일:2007-07-05 10:49:14
by 좋은진호

apache에서는 mod_expires 모듈을 통해 Expires HTTP header 를 설정할 수 있다.

이를 통하여 클라이언트(웹페이지 방문자)에 캐싱되는 문서나 이미지들이 많아서 트래픽을
감소시킬 수 있다. 이미지 전용 서버나 이미지 디렉토리에 설정을 해두면 효과적이다.

이미지 서버에 지정한 다음 예를 보자.


<IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 month"

# 제외할 디렉토리
        <Directory "/usr/local/apache/htdocs/temp">
        ExpiresActive Off
        </Directory>
</IfModule>


/usr/local/apache/bin/apxs -aic mod_expires.c
- ExpiresActive On 지시자로 Expires 설정을 enable 한다.
- ExpiresDefault "access plus 1 month" 지시자는 액세스한지 얼마나 지나서 expire할 것인지를 지정한다.
  즉, 지정한 기간만큼 클라이언트에 캐싱이 된다. 위에는 1달이다.

이외에 클라이언트에서 액세스한지 1달, 4주, 30일, 1년 등과 같은 expire 주기와
서버의 파일의 수정 시간으로 expire 주기를 설정할 수 있다.

 


ExpiresDefault "access plus 1 month"
ExpiresDefault "access plus 4 weeks"
ExpiresDefault "access plus 30 days"
ExpiresDefault "access plus 1 years"
ExpiresDefault "modification plus 30 days"

 

 

- 설정 마지막부분에 Directory 지시자와 ExpiresActive Off 설정을 통해

  특정 디렉토리만 expire 설정에서 제외할 수 있다.

  반대로 특정 디렉토리만 On으로도 설정할 수 있다. (일반 웹서버에 /images 와 같이 디렉토리가 있는 경우)

 


ExpiresByType image/jpeg "acces plus 4 weeks"
ExpiresByType image/gif  "acces plus 4 weeks"

 

 

- 위처럼 파일의 유형(image/jpeg, image/gif은 이미지 파일)으로도 가능하다.

 

아주 간단하지 않는가?

 

참고로 [다음(daum)] 의 이미지 서버는 28일(4주)로 [야후!코리아] 는 5년으로 설정되어 있다.


* 참고 자료 : http://httpd.apache.org/docs/mod/mod_expires.html

            http://httpd.apache.org/docs/2.0/mod/mod_expires.html

  • 이 문서는 http://coffeenix.net 의 좋은진호님의 동의하에 등록된 글입니다.

 

 

 

다른 방법으로는

httpd.conf 에 간단히

ExpiresActive On
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"

요렇게 해 주면 js, css, images 는 브라우저 캐시를 이용하게 됩니다.

그러면 한번 불러온 저 파일들은 뒤로,앞으로 이동시에 다시 불러오지 않습니다.
오직 새로고침시에만 다시 불러옵니다.

License : Public Domain

 

압축 전송 : http://httpd.apache.org/docs/2.2/mod/mod_deflate.html

 

 

 

 

  -php 로 헤더 보내기

css 기준 입니다.

$etag->mtime = filemtime($cssfile);
$etag->inode = fileinode($cssfile);
$etag->size = filesize($cssfile);
$apacheheader = apache_request_headers();
if(isset($apacheheader['If-Modified-Since'])) {
    $ims = strtotime(preg_replace('/;.*$/', '', $apacheheader['If-Modified-Since']));
    if($etag->mtime == $ims) {
        header('HTTP/1.1 304');
        $etag->end = true;
    }
}
header('Date: '.substr(gmdate('r'), 0, -5).'GMT');
header('Expires: '.substr(gmdate('r', strtotime('+1 MONTH')), 0, -5).'GMT');
header('Cache-Control: private, max-age=2592000');
header('Pragma: cache');
header('Last-Modified: '.substr(gmdate('r', $etag->mtime), 0, -5).'GMT');
header('ETag: '.dechex($etag->inode).'-'.dechex($etag->size).'-'.dechex($etag->mtime));
if(isset($etag->end)) exit;
echo file_get_contents($cssfile);


중요하게 봐야 할 것은
1. 시간은 무조건 GMT
2. Cache-Control 의 private 명시
3. ETag 형식
4. If-Modified-Since -> 304

입니다.
여기서 ETag 는 그 파일을 구분할 수 있는 고유 코드가 생성되기만 하면,
다른 문자열이라도 상관 없습니다.


css 나 js 의 경우 private 를 걸지 않아도 304 가 나올만한 요청 자체를 안합니다.
이 요청을 하게 하는것이 Cache-Control 의 must-revalidate 입니다.
굳이 할 필요는 없겠지요.
특히 IE6 의 경우 매번 css 를 받아오게 되면 css를 못받아오고 쌩 html 이 출력되는 경우가 빈번합니다.

html 의 경우 private 는 하면 안됩니다.
위의 헤더만 잘 뿌려주면 If-Modified-Since 헤더를 보내기 때문에 304 를 뿌릴 수 있습니다.

패킷분석툴은 wireshark 가 좋습니다.

License : BSD 

이 글은 스프링노트에서 작성되었습니다.

.

'Computer > LINUX' 카테고리의 다른 글

로그 제어하기  (0) 2012.08.10
Openssl 설치법  (0) 2012.08.10
mod_cband (apache 2.x) 다른거  (0) 2012.08.10
mod_cband (apache 2.x)  (0) 2012.08.10
httpd.conf  (0) 2012.08.10