Showing posts with label Apache. Show all posts
Showing posts with label Apache. 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>

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.