Wednesday, May 8, 2013

Speed up your webservers


Here I will explain how to optimize a webserver for faster communication between the server and browser clients using compression and caching methods.

APACHE
Apache achives this by gziping the webcontents before transfer using deflate module and by enabling caching of static files at client browsers for a particular period.

For compressing ..

Load deflate module to apache using apxs utility by issuing the following command,

apxs -c -i -a mod_deflate.c

Load header module by issuing the following command,

apxs -c -i -a mod_headers.c

restart apache, and list out the loaded modules

httpd -M
deflate_module (shared)
headers_module (shared)

It should list these two modules.

Now , we can add the following configuration inside the virtual hosting parameter.

<IfModule mod_deflate.c>
  SetOutputFilter DEFLATE
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  BrowserMatch \bOpera !no-gzip
  SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip)$ no-gzip dont-vary
 <IfModule mod_headers.c>
 Header append Vary User-Agent env=!dont-vary
 </IfModule>
</IfModule>


For Caching ..

Load expires module by issuing the following command,

apxs -c -i -a mod_expires.c


Load header module by issuing the following command,

apxs -c -i -a mod_headers.c


restart apache, and check for the new modules.

httpd -M
expires_module (shared)
headers_module (shared)

Now , we can add the following configuration inside the virtual hosting parameter.


<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 seconds"
    ExpiresByType image/x-icon "access plus 2692000 seconds"
    ExpiresByType image/jpeg "access plus 2692000 seconds"
    ExpiresByType image/png "access plus 2692000 seconds"
    ExpiresByType image/gif "access plus 2692000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"
    ExpiresByType text/css "access plus 2692000 seconds"
    ExpiresByType text/javascript "access plus 2692000 seconds"
    ExpiresByType application/x-javascript "access plus 2692000 seconds"
    ExpiresByType text/html "access plus 600 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
<IfModule mod_headers.c>
    <FilesMatch "\\.(ico|jpe?g|png|gif|swf|css|js)$">
        Header set Cache-Control "max-age=2692000, public"
    </FilesMatch>
    <FilesMatch "\\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
    </FilesMatch>
    Header unset ETag
    Header unset Last-Modified
</IfModule>


We can give the caching duration as seconds, week, month as shown in the configuration above.
The `must-revalidate`  that we are passing through headers, validates for any changes rather than blind caching of files.



NGINX

For compressing use the below configuration,

  gzip on;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_min_length 0;
  gzip_buffers 16 8k;
  gzip_proxied any;
  gzip_types text/plain application/x-javascript application/javascript text/css text/xml text/javascript application/xml application/xml+rss application/json;
  gzip_disable "MSIE [1-6]\.";
  gzip_vary on;


For caching use the below sample configuration,


location ~ .(css|js)$ {

        expires 604800s;

        add_header Pragma .public.;

       add_header Cache-Control .public,must-revalidate,proxy-revalidate.;
}

location ~ .(txt|xml)$ {

        expires 3600s;

        add_header Pragma .public.;

       add_header Cache-Control .public,must-revalidate,proxy-revalidate.;
}

location ~ .(bmp|gif|ico|jpg|JPG|jpeg|jpe|swf|tif|tiff|png)$ {

        expires 2592000s;

        add_header Pragma .public.;

       add_header Cache-Control .public,must-revalidate,proxy-revalidate.;

}


As of my experience for compression and caching, nginx is more better than apache.
So if you are using apache, it would be better if you use nginx as a reverse proxy in front of it.



Cheers /-

No comments:

Post a Comment