Skip to content

Instantly share code, notes, and snippets.

@hiz8
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save hiz8/fd32264afff05e53af1b to your computer and use it in GitHub Desktop.

Select an option

Save hiz8/fd32264afff05e53af1b to your computer and use it in GitHub Desktop.
mod_expires マニュアル

Apacheのmod_expiresを利用したキャッシュコントロール

mod_expiresの書式

ExpiresDefault "<base> [plus] {<num> <type>}*"
ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
<ifModule mod_expires.c>
     ExpiresActive On
     ExpiresByType image/jpeg "access plus 1 months"
</ifModule>

1行目「mod_expires」がある場合は以下の項目を実行という指定。
2行目「ExpiresActive On」でモジュールを有効に設定。
3行目「ExpiresByType」と記述した後にMINIタイプを指定することでキャッシュを有効にするファイルタイプを指定することができます。
「access plus 1 months」はアクセスしてから1カ月間キャッシュしたデータを利用するという指定です。

「ExpiresDefault」と「ExpiresByType」の違い

「ExpiresDefault」は全てのファイルを対象にします。例えば以下の設定だとすべてのファイルのキャッシュ期間が1カ月になります。

ExpiresDefault "modification plus 1 month"
<FilesMatch "\.(jpg|jpeg)$">
     ExpiresDefault "modification plus 1 month"
</FilesMatch>

type/encodingの書式

「MIME-type」を指定します。
MIMEタイプ一覧

[plus]の書式

「base」で指定するのは以下の3つです。

  • アクセスしてからの時間を指定する「acces」。
  • 上記と同じ「now」。
  • ファイルの更新日時から時間を指定する「modification」。

つまり「acces」と「now」は実質1つなので、アクセスしてからの時間でキャッシュ期間を決めるのか、ファイルの更新日時からキャッシュ期間を決めるのかを決めるということになります。

「plus」は省略可能で、合ってもなくても問題ありません。

の書式

「num」は整数、typeは以下のものが使えます。

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

1カ月なら「1 months」、「4 weeks」など。
1日なら「1 day」、「24 hours」など。

<ifModule mod_expires.c>
     ExpiresActive On
     ExpiresByType image/png "access plus 1 months"
     ExpiresByType image/jpeg "access plus 1 months"
     ExpiresByType image/gif "access plus 1 months"
     ExpiresByType text/css "access plus 1 months"
</ifModule>
<IfModule mod_expires.c>
     ExpiresActive on

     ExpiresByType image/jpg "access plus 60 days"
     ExpiresByType image/png "access plus 60 days"
     ExpiresByType image/gif "access plus 60 days"
     ExpiresByType image/jpeg "access plus 60 days"

     ExpiresByType text/css "access plus 1 days"

     ExpiresByType image/x-icon "access plus 1 months"

     ExpiresByType application/pdf "access plus 1 months"
     ExpiresByType audio/x-wav "access plus 1 months"
     ExpiresByType audio/mpeg "access plus 1 months"
     ExpiresByType video/mpeg "access plus 1 months"
     ExpiresByType video/mp4 "access plus 1 months"
     ExpiresByType video/quicktime "access plus 1 months"
     ExpiresByType video/x-ms-wmv "access plus 1 months"
     ExpiresByType application/x-shockwave-flash "access 1 months"

     ExpiresByType text/javascript "access plus 1 weeks"
     ExpiresByType application/x-javascript "access plus 1 weeks"
     ExpiresByType application/javascript "access plus 1 weeks"
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment