Showing posts with label Front End Optimization. Show all posts
Showing posts with label Front End Optimization. Show all posts

Tuesday, February 3, 2015

mod_expires in apache

This module controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. The expiration date can set to be relative to either the time the source file was last modified, or to the time of the client access.
These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.

Sample Configuration:

<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 "modification plus 10 days"
 ExpiresByType image/x-icon "access plus 10 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType audio/x-wav "access plus 1 month"
 ExpiresByType audio/mpeg "access plus 1 month"
 ExpiresByType video/mpeg "access plus 1 month"
 ExpiresByType video/mp4 "access plus 1 month"
 ExpiresByType video/quicktime "access plus 1 month"
 ExpiresByType video/x-ms-wmv "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType text/javascript "access plus 1 week"
 ExpiresByType application/x-javascript "access plus 1 week"
 ExpiresByType application/javascript "access plus 1 week"
</IfModule>

mod_deflate in apache

mod_deflate module in apache is used to compress the HTTP response before sending it to client. It does not have a lower bound for file size, so it attempts to compress files that are too small to benefit from compression. This results in files smaller than approximately 120 bytes becoming larger when processed by mod_deflate.

Sample Configuration:

<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript

 DeflateCompressionLevel 5

# Browser specific settings
 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

# Setup custom deflate log
 DeflateFilterNote Input instream
 DeflateFilterNote Output outstream
 DeflateFilterNote Ratio ratio

 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
# Example of log file
 CustomLog logs/deflate_log DEFLATE
</IfModule>

ETag and its working

Etag is a mechanism that HTTP provides for web cache validation, and allows a client to make conditional requests.

Working:

When a URL is retrieved, the web server will return the resource along with its corresponding ETag value.  On the subsequent request, the server now compare the ETag value of the resource in the request with the ETag for the current version of the resource in the server. If the ETag values match, meaning that the resource has not changed, then the server send back a very short response with a HTTP 304 Not Modified status. The 304 status tells the client that its cached version is still good and that it should use that. However, if the ETag values do not match, meaning the resource has likely changed, then a full response including the resource's content is returned. In this case the client may decide to replace its previously cached version with the newly returned resource and the new ETag.

Note - If you are using a load-balanced server setup with multiple machines running Apache you will probably want to turn off ETag generation. This is because inodes are used as part of the ETag hash algorithm which will be different between the servers. You can configure Apache to not use inodes as part of the calculation but then you'd want to make sure the timestamps on the files are exactly the same, to ensure the same ETag gets generated for all servers.

Monday, February 2, 2015

Prefork vs Worker in Apache

Everyone knows about apache and most of us also have hands on experience with apache. Today let us see multi processing modules in it. Apcahe comes with 2 multi processing modules(MPMs):

1. Prefork
2. Worker

The defalut MPM is prefork.

What is the difference between this two?
  • Prefork MPM uses multiple child processes with one thread each and each process handles one connection at a time.
  • Worker MPM uses multiple child processes with many threads each. Each thread handles one connection at a time.
On most of the systems, speed of both the MPMs is comparable but prefork uses more memory than worker.

Which one to use?
On high traffic websites worker is preferable because of low memory usage as comparison to prefork MPM but prefork is more safe if you are using libraries which are not thread safe.
For example, you cannot use mod_php with worker MPM as it is not thread safe.
So if you are using all thread safe libraries then go with worker and if you are not sure then use prefork MPM, you may have to increase your RAM in case of high traffic.