Apache set Expiry date for javascript files and images

I started looking into page caching this after running Google Page Speed Insights to make my website load faster and one of the recommendations was "Specify an expiration at least one week in the future for the following resources". If your web server is apache then you can easily configure this in the httpd.conf file using the Apache module mod_expires, this will allow the browsers to cache the specific resources for the given time limit and speed up the page loading when same user gets to your website more than once. Here is code sample
<IfModule mod_expires.c>
   ExpiresActive On
   ExpiresByType image/jpg "access 2 month"
   ExpiresByType image/jpg "access 2 month"
   ExpiresByType image/gif "access 2 month"
   ExpiresByType image/jpg "access 2 month"
   ExpiresByType image/jpeg "access 2 month"
   ExpiresByType image/png "access 2 month"
   ExpiresByType text/css "access 2 month"
   ExpiresByType application/x-javascript "access plus 2 month"
   ExpiresByType text/javascript "access plus 2 month"
   ExpiresByType application/javascript "access plus 2 month"
   ExpiresByType image/x-icon "access plus 12 month"
   ExpiresByType image/icon "access plus 12 month"
   ExpiresByType application/x-ico "access plus 12 month"
   ExpiresByType application/ico "access plus 12 month"
You can check the HTTP response header to see if the changes are in effect. Here is screen shot from chrome developer tools...
Apache set Expiry date for javascript files and images

If you are using Virtual Private Hosting(VPS) with Plesk then you must change or create the vhost.conf and vhost_ssl.conf files in the conf folder of your domain dir. In my case the full path was
  • /var/www/vhosts/demo.mysamplecode.com/conf/vhost.conf 
  • /var/www/vhosts/demo.mysamplecode.com/conf/vhost_ssl.conf

After you have done editing, for these changes to take effect run the following command
/usr/local/psa/admin/bin/httpdmng --reconfigure-domain {your_domain_name}

Here is an example based on my website
/usr/local/psa/admin/bin/httpdmng --reconfigure-domain demo.mysamplecode.com


No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.