Answer the question
In order to leave comments, you need to log in
How to set up proper caching in Apache?
Hello fellow toasters!
I found a strange problem with caching of images, css-, js-files on the site under development.
I use LAMP on the server:
- Debian GNU/Linux 7.5 (wheezy).
- Apache 2.2.22 (loaded modules: alias, auth_basic, authn_file, authz_default, authz_groupfile, authz_host, authz_user, autoindex, cgid, deflate, dir, env, expires, fcgid, headers, mime, negotiation, reqtimeout, rewrite, setenvif, ssl , status, suexec, unique_id, xsendfile).
- PHP 5.4.4-14+deb7u11 (cgi-fcgi).
In the directory with images, I placed the .htaccess file with the following content:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
Header append Cache-Control "public"
</IfModule>
GET /images/some_image.png HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: https://example.com/
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
HTTP/1.1 200 OK
Date: Tue, 01 Jul 2014 10:33:49 GMT
Server: Apache/2.2.22 (Debian)
Last-Modified: Mon, 09 Jun 2014 19:29:59 GMT
Etag: "2016e1-50b-4fb6c3ca9a6b1"
Accept-Ranges: bytes
Content-Length: 1291
Cache-Control: max-age=2592000, public
Expires: Thu, 31 Jul 2014 10:33:49 GMT
X-Frame-Options: sameorigin
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: image/png
GET /images/some_image.png HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: https://example.com/
Connection: keep-alive
If-Modified-Since: Mon, 09 Jun 2014 19:29:59 GMT
If-None-Match: "2016e1-50b-4fb6c3ca9a6b1"
Cache-Control: max-age=0
HTTP/1.1 304 Not Modified
Date: Tue, 01 Jul 2014 10:48:10 GMT
Server: Apache/2.2.22 (Debian)
Connection: Keep-Alive
Keep-Alive: timeout=5, max=98
Etag: "2016e1-50b-4fb6c3ca9a6b1"
Expires: Thu, 31 Jul 2014 10:48:10 GMT
Cache-Control: max-age=2592000, public
Answer the question
In order to leave comments, you need to log in
Maybe so, as a universal solution.
<ifModule mod_headers.c>
#кэшировать html и htm файлы на один день
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=43200"
</FilesMatch>
#кэшировать css, javascript и текстовые файлы на одну неделю
<FilesMatch "\.(js|css|txt)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
#кэшировать флэш и изображения на месяц
<FilesMatch "\.(flv|swf|ico|gif|jpg|jpeg|png)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
#отключить кэширование
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
</IfModule>
<ifModule mod_expires.c>
ExpiresActive On
#по умолчанию кеш в 5 секунд
ExpiresDefault "access plus 5 seconds"
#кэшировать флэш и изображения на месяц
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
#кэшировать css, javascript и текстовые файлы на одну неделю
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 604800 seconds"
ExpiresByType application/javascript "access plus 604800 seconds"
ExpiresByType application/x-javascript "access plus 604800 seconds"
#кэшировать html и htm файлы на один день
ExpiresByType text/html "access plus 43200 seconds"
#кэшировать xml файлы на десять минут
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
Why don't you want to distribute statics and cache through the frontend on nginx?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question