Skip to content

Сжатие контента с помощью gzip/deflate apache2

Для включения кеширования нам нужен модуль mod_deflate, для кеширования — mod_headers или mod_expires. Также mod_headers вам пригодится чтобы удалять заголовок ETag, он лишний и могут быть проблемы с кешированием через mod_header при включенном сжатии. Для включение выполните:

a2enmod deflate headers expires

Далее перезапускаем apache2:

service httpd restart

или

service apache2 restart

Сжать ответ сервера для перечисленных MIME типов

### Сжать ответ сервера для перечисленных MIME типов
<ifModule mod_deflate.c>
  <IfModule mod_filter.c>
      AddOutputFilterByType DEFLATE text/plain text/html
      AddOutputFilterByType DEFLATE text/css
      AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
      AddOutputFilterByType DEFLATE text/xml application/xml application/xhtml+xml application/rss+xml
      AddOutputFilterByType DEFLATE application/json
      AddOutputFilterByType DEFLATE application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon
  </ifModule>
</ifModule>

### Подсказать браузеру схему кеширования через заголовки в ответе сервера
<ifModule mod_headers.c>
    # 43200 - день, 604800 - неделя, 2592000 - месяц
    <FilesMatch "\.(html|js|css)$">
	Header set Cache-Control "max-age=2592000"
        #Header unset Last-Modified
    </FilesMatch>
    <Files *.txt>
	Header add Cache-Control "max-age=43200"
    </Files>
    <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>
    # Enable expires
    ExpiresActive On
    
    # Default a cache expiration
    ExpiresDefault "access plus 10 month"
    
    # Images
    ExpiresByType image/gif                 "access plus 1 month"
    ExpiresByType image/png                 "access plus 1 month"
    ExpiresByType image/jpg                 "access plus 1 month"
    ExpiresByType image/jpeg                "access plus 1 month"
    
    # CSS, JavaScript
    ExpiresByType text/css                  "access plus 1 year"
    ExpiresByType application/javascript    "access plus 1 year"
    ExpiresByType text/javascript           "access plus 1 year"
</IfModule>

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$">
  Header set Cache-Control "max-age=2592000"
</FilesMatch>

Проверка http://checkgzipcompression.com/

Published inLinux

Be First to Comment

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *