Rewrite / Compres / Expires

sudo a2enmod expires
sudo a2enmod deflate
sudo a2enmod rewrite
Options +FollowSymLinks +ExecCGI
 
<IfModule mod_rewrite.c>
  RewriteEngine On
 
  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
  #RewriteBase /
 
  # we skip all files with .something
  #RewriteCond %{REQUEST_URI} \..+$
  #RewriteCond %{REQUEST_URI} !\.html$
  #RewriteRule .* - [L]
 
  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f
 
  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
 
 
<IfModule mod_deflate.c>
  # Insert filter
  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript application/json
  SetOutputFilter DEFLATE
 
 
  # MSIE masquerades as Netscape, but it is fine
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 
  # Don't compress images
  SetEnvIfNoCase Request_URI \
   \.(?:gif|jpe?g|png)$ no-gzip dont-vary
</IfModule>
 
<FilesMatch "\.(css|js)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

basic performance settings Options -Indexes ExpiresActive on ExpiresDefault "access plus 1 month" AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript

Caching with mod_headers

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=29030400, public"
</FilesMatch>
 
# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
 
# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</FilesMatch>
 
# NEVER CACHE
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

caching with mod expires

 
FileETag None
 
<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType image/jpg "access plus 30 day"
  ExpiresByType image/gif "access plus 30 day"
  ExpiresByType image/jpg "access plus 30 day"
  ExpiresByType image/jpeg "access plus 30 day"
  ExpiresByType image/png "access plus 30 day"
  #
  ExpiresByType text/css "access plus 30 day"
  # js
  ExpiresByType application/x-javascript "access plus 30 day"
  ExpiresByType text/javascript "access plus 30 day"
  ExpiresByType application/javascript "access plus 30 day"
  # icon
  ExpiresByType image/x-icon "access plus 30 day"
  ExpiresByType image/icon "access plus 30 day"
  ExpiresByType application/x-ico "access plus 30 day"
  ExpiresByType application/ico "access plus 30 day"
</IfModule>

togliere Etags e Last modified headers

toglie del tutto la richiesta al file per determinare se il file è modificato, per cui varrà l'indicazione di cache-controll

Header unset ETag
FileETag None
<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
</filesMatch>

Compress Components With mod_deflate

As an alternative to compressing files with Gzip, you can use mod_deflate(which is supposively faster). Place the following code at the top of your .htaccess file(tip: you can also add .jpg|.gif|.png|.tiff|.ico mod_deflate those): view sourceprint?

    <ifmodule mod_deflate.c="">
    <filesmatch .(js|css)$"="">
    SetOutputFilter DEFLATE
    </filesmatch>
    </ifmodule>

Add this setting to your php.ini file:

zlib.output_compression=1
 
; minimal compression, less CPU
zlib.output_compression_level=1
 
; maximal compression, more server CPU time must be used
zlib.output_compression_level=9

Benchmark

  ab -c 10 -t 60 http://domain/
  siege -u shemp.whoohoo.com/Admin.jsp -d1 -r10 -c25

per siege, i parametri da tenere d'occhio sono Response time: 0.09 secs Transaction rate: 56.84 trans/sec

Add Expires Headers

The following code shows you how to add an expiration date on the headers.

    <filesmatch .(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"="">
    Header set Expires "Wed, 21 May 2010 20:00:00 GMT"
    </filesmatch>

Force Caching

The following code will not directly increase the loading speed of your website. What it will do is, load the content of your site faster when the same user revisits your website by sending 304 status when requested components have not been modified. You can change the cache expiry by changing the number of seconds(it's currently set at 1 day).

FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"

Compress Components By Enabling Gzip

By making use of Gzip you can compress files in order to make your website load faster.

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
 
BrowserMatch bMSIE !no-gzip !gzip-only-text/html

MOD pagespeed

https://developers.google.com/speed/docs/mod_pagespeed/install#module

mod_cache

sudo a2enmod cache && sudo a2enmod disk_cache
/etc/init.d/apache2 force-reload
apt-get install apache2-utils # htcacheclean
# /etc/rc.local
/usr/sbin/htcacheclean -d30 -n -t -p /var/cache/apache2/mod_disk_cache -l 100M -i
<IfModule mod_cache.c>
<IfModule mod_disk_cache.c>
CacheDefaultExpire 3600
CacheEnable disk /
CacheRoot "/opt/apicache/"
CacheDirLevels 2
CacheDirLength 1
CacheMaxFileSize 1000000
CacheMinFileSize 1
CacheIgnoreCacheControl On
CacheIgnoreNoLastMod On
CacheIgnoreQueryString Off
CacheIgnoreHeaders None
CacheLastModifiedFactor 0.1
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheStoreNoStore On
CacheStorePrivate On
</IfModule>
</IfModule>

testing dell funzionamento cache, in un file /var/www/cachetest.php scrivere il time()

<?php
header
("Cache-Control: must-revalidate, max-age=300");
header("Vary: Accept-Encoding");
echo 
time()."<br>";

ottimizzazione della partizione in scrittura:

/dev/md1   /opt   ext3   defaults,noatime,nodiratime,data=writeback 1 2

trovare i files .htaccess

hanno un impatto sulle performance, se possibile configurare il server

  find . -name *.htaccess -print