Browser caching after deploy - apache

I have this caching commands on my server:
# BEGIN Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 2 days"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType text/css "access plus 7 days"
ExpiresByType text/javascript "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
</IfModule>
# END Expire headers
I've checked and the caching is working fine. I have Capistrano set up for deploying to server, my question is what will happen after deploy? Will the browser cache the files again because they changed the path on the server or user will have to wait for time to expire?
Thanks...

my question is what will happen after deploy? Will the browser cache the files again because they changed the path on the server or user will have to wait for time to expire?
Unfortunately users will have to wait for browsers to expire the cached data since browsers have no way to know whether these files have changed. However if you can change the src paths of these files then browser will hit the server again and fetch a fresh copy.

Related

Managing/Updating headers in Apache Server

We are using Apache as our Web server for our CRM application (Siebel) . We continuosly deploy new JS and CSS files on the CRM application in 15 days Currently we have the following setting.
FileETag None
ExpiresActive On
<IfModule mod_expires.c>
ExpiresByType text/css "access plus 5 days"
ExpiresByType text/javascript "access plus 5 days"
ExpiresByType image/gif "access plus 5 days"
ExpiresByType image/jpg "access plus 5 days"
ExpiresByType image/png "access plus 5 days"
ExpiresByType application/x-shockwave-flash "access plus 5 days"
ExpiresByType application/x-javascript "access plus 5 days"
AddType image/x-icon .ico
ExpiresByType image/x-icon "access plus 5 days"
</IfModule>
In this case if we have a Hotfix release we have to ask the end users to clear their cache so that the new files can be reflected from their side else they have to wait till the objects expire and are auto refreshed.
If we set it to a lower value than the requests to the web server increase and the load increases on web server.
I would like to know is there any mechanism.
1) To auto update the new files on end user machine automatically when a new file is placed .
2) To set expiration of folder level rather than a file level.
Thanks
In response to point 1)
I'd suggest using versioning at the URL level.
I.E mysite.com/lib/js.js?v=1, mysite.com/lib/style.css?v=1 (etc.. etc..)
When you roll out a new version, update this to v=x (and so on).
There is no way of forcing a client to flush it's cache, mainly due to all the browsers being implemented different (they'd all have to implement this), and it would no doubt be some risk of security.
As for point 2) maybe this question might help:
ServerOverflow - Apache: ExpiresByType per directory?

Leverage browser caching on Amazon EC2

This is my first time using Amazon AWS for any hosting and I've uploaded my usual code, below, to help with browser caching and it seems that tools like GT Metrix and Google Page speed are not seeing it work.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
Any ideas if I need to enable anything on Amazons side for this to work?
Thanks in advance.
After some investigation it turned out that the Apache modules needed for this were not installed on our instance.
What you need to do is ssh into your server as a root user and run the following command
Check which modules are installed using this command
apache2ctl -M
and looking for expires_module. It's probably not there.
enable browser caching
sudo a2enmod expires
restart apache
sudo service apache2 restart

Apache not caching static content

I read the official caching guide of the latest Apache httpd version, but did not understand how to get a minimal caching setup for static content.
Googling around, I finally added these rules to my /etc/apache2/apache2.conf (I'm using Ubuntu):
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</IfModule>
<IfModule mod_headers.c>
Header set Cache-Control "public"
</IfModule>
FileETag None
Obviously, I already enabled expires, headers, cache modules.
When I try to access an image, a css or js file, I see 200 OK the first time, and 304 the next ones. So, I thought I was right... but Google Pagespeed (for example) still complains about files that are not cached.
Actually, I had some suspects that I'm missing something:
I didn't activate mod_cache or mod_cache_disk. Should I? What's the basic set of rules for doing so?
Why is it necessary to disable the ETag?
I absolutely need my cache to expire suddenly when the file is changed: ideally, the expiration time (for css, js) could be very long, let's say, two weeks, but if the file changes after one hour, the user should mandatory get the updated file! Is that behavior automatically managed by Apache?
I absolutely need my cache to expire suddenly when the file is changed
TWhen you use mod_expires to send an Expires header the client doesn't have to make sure the file is fresh and you can't force a change ever.
If you drop mod_expires, your static files will have an ETAG and a last-modified-time which allows browsers to make sure the file hasn't changed (these are the 304 responses).
You'll need to a) scrutinize the pagespeed messages more closely B) assess them against your requirement and C) look at real world traffic in your access log wrt 304s.
You do not want mod_cache for static files.

Setting ExpiresByType for entire server

I run a server with several websites on it. I would like to implement default cache control behavior for all of these websites. Can I just do this by adding the following code to httpd.conf or must I make the changes in the configuration of each virtual host separately?
<IfModule mod_expires.c>
ExpiresActive on
# Your document html
ExpiresByType text/html "access plus 12 hours"
# Media: images, video, audio
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"
# CSS and JavaScript
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
# Default
ExpiresDefault "access plus 1 month"
</IfModule>
Documentation can be found in the Apache HTTP Server web site. In the "Documentation" section on the left pane, click on your server version. I'll assume 2.4. Since you are looking for reference on specific directives, you can click on the Directives link. You'll get an alphabetical index.
I won't copy the complete information, just a little sample for ExpiresActive. The important bit is the top table:
Description: Enables generation of Expires headers
Syntax: ExpiresActive On|Off
Default: ExpiresActive Off
Context: server config, virtual host, directory, .htaccess
Override: Indexes
Status: Extension
Module: mod_expires
At "Context" we can read that the directive can be set at several places, including server config. So this answers your question: in theory, it should work. You should check the rest of the directives in order to make sure (or just test it).

Implementing cache control using .htaccess on Apache server

okay, I'm still trying to get my head around some of the caching stuff and I have gone through a couple of examples I could find on Google. I have added the following code to my .htaccess file:
### activate mod_expires
ExpiresActive On
### Expire .gif's 1 month from when they're accessed
ExpiresByType image/gif "access plus 3 months"
ExpiresByType image/png "access plus 3 months"
ExpiresByType image/jpg "access plus 3 months"
ExpiresByType text/javascript "access plus 3 months"
Using the Chrome audit tools and the YSlow Firebug tool, it looks like this is caching some of my images/files, but not by far all of them. I still have a list of files (.jpg, .js and .css - I know I've not set the css files to cache here) that aren't caching. The message in the Chrome audit simply states The following resources are missing a cache expiration. Resources that do not specify an expiration may not be cached by browsers:
some of the images that aren't caching are background images, others are part of a js gallery and they're being called via the JS - could that be affecting why they aren't caching?
Sorry I can't give a link to the code - the sites still under wraps and limited to client view only.
Thanks in advance!
It looks like you've written the MIME-types wrong:
# enable expirations
ExpiresActive On
ExpiresDefault "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/pjpeg "access plus 1 week"
ExpiresByType text/javascript "modification plus 1 week"
ExpiresByType application/javascript "modification plus 1 week"
ExpiresByType text/css "modification plus 1 week"