TJ_Vreugdenhil
Aug 06, 2014Cirrus
expires header for images iRule v11.x
For all images, we need to implement the expires tag to tell the users’ browser if they already have the image, that they can use that cached version for X hours.
For images, the expires tag should live for 7 days. For css/js files, the expires tag should live for 24 hours.
Does anyone have an example iRule that will do this?
I found these two links which look close :
Would there be a simpler way to do this rather then the examples here? Would both of these work for 11.x?
https://devcentral.f5.com/wiki/iRules.Expires-iRule.ashx https://devcentral.f5.com/questions/irule-to-set-expires-headers-for-static-conent
Would something like this work?
86400 seconds = 24 hours
604800 seconds = 7 days
ltm data-group internal /Common/class_cacheable_file_types{
records {
".css 86400"
".js 86400"
".bmp 604800"
".gif 604800"
".jpeg 604800"
".jpg 604800"
".png 604800"
".ico 604800"
}
type string
}
rule irule_manipulate_client_cache {
when HTTP_REQUEST {
Set the cache timeout for root requests. Check that the request is a GET and that there is not a query string.
if {[HTTP::method] eq "GET" and [HTTP::uri] eq "/"}{
set expire_content_timeout 300
} else {
Set the tiemout based on the class entry if it exists for this request.
set expire_content_timeout [class match [string tolower [string range [HTTP::path] [string last . [HTTP::path]] end]] class_cacheable_file_types " "]
}
}
when HTTP_RESPONSE {
if { $expire_content_timeout ne "" } {
HTTP::header replace "Cache-Control" "max-age=$expire_content_timeout, public"
HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+$expire_content_timeout)] -format "%a, %d %h %Y %T GMT" -gmt true]"
} else {
HTTP::header replace "Cache-Control" "private"
HTTP::header replace Expires {-1}
}
}
}
Thanks!