Forum Discussion
Jeff_C_42204
Nimbostratus
Aug 01, 2007Irule to set expires headers for static conent
Hello Dev Central!
I have an IIS 6.0 server that is not setting the expires header (cache-control is set) which is causing a potential performance issue.
I would like to remedy this by adding the expires header for extensions listed in the images data group.
The following snippet successfuly returns a header "JeffTest" that is 24 hours in the futurein GMT format; so that portion should work just fine for the expires field.
set cachetime 86400
HTTP::header insert JeffTest "[clock format [expr ([clock seconds]+$cachetime)] -format "%a, %d %h %Y %T GMT" -gmt true]"
The problem I am having is that I need it to only be inserted for static content as defined in my images group. I tried the followign which I hoped would work but did not. Is the http:uri function nto allowed in http response? Or is it possible my brackets/bracing are just wrong?
when HTTP_RESPONSE {
if { [matchclass [HTTP::uri] ends_with $::images]} {
set cachetime 86400
HTTP::header insert JeffTest "[clock format [expr ([clock seconds]+$cachetime)] -format "%a, %d %h %Y %T GMT" -gmt true]"
}
If the http::uri function is not allowed in http_response does that mean I have to set two parts to the irule: the first during http::request which matches class and then sets a flag and a second correponding http_response which checks for that flag and if present inserts the header?
Thanks ahead of time for the help!
10 Replies
- Jeff_C_42204
Nimbostratus
Ok I found reference in the following article that http::uri function is not allowed in http_response. It also shows that a session variable can be created to move the data you want into the response. I'm still working on this and it probably is not quite syntactically correct but let me know if you guy see anything glaring or if I seem to be on the right path. - Jeff_C_42204
Nimbostratus
I was trying to make things a little harder than they needed to be, here is an irule that works: - bl0ndie_127134Historic F5 AccountYou might want to do the check in the request so that you can store just a boolean value instead of the whole uri. It might be a good thing to do if you uri tend to be long.
- Jeff_C_42204
Nimbostratus
Good thought -- the smaller the stored value the better. I'll edit the rule and do a little bit of testing. - This should work for you.
when HTTP_REQUEST { set expire [matchclass [HTTP::uri] ends_with $::images] } when HTTP_RESPONSE { if { $expire == 1 } { HTTP::header insert Expires "[clock format [expr ([clock seconds]+86400)] -format "%a, %d %h %Y %T GMT" -gmt true]" } }
- Rodrigo_EV_7869
Nimbostratus
As more customers are beginning to use YSlow (http://developer.yahoo.com/yslow/ - a Firefox add-on integrated with the popular Firebug web development tool that analyzes web pages and tells you why they're slow based on the rules for high performance web sites) to better develop their sites and applications, I think this topic can become a little bit more important.when RULE_INIT { 300s=5min, 3600s=1hour, 86400s=1day, 604800=1week set expire_content_300 0 set expire_content_3600 0 set expire_content_86400 0 set expire_content_604800 0 } when HTTP_REQUEST { set expire_content_300 [matchclass [string tolower [HTTP::path]] ends_with ::class_expire_content_300] set expire_content_3600 [matchclass [string tolower [HTTP::path]] ends_with ::class_expire_content_3600] set expire_content_86400 [matchclass [string tolower [HTTP::path]] ends_with ::class_expire_content_86400] set expire_content_604800 [matchclass [string tolower [HTTP::path]] ends_with ::class_expire_content_604800] } when HTTP_RESPONSE { if { $expire_content_300 == 1 } { HTTP::header replace "Cache-Control" "max-age=300" HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+300)] -format "%a, %d %h %Y %T GMT" -gmt true]" } elseif { $expire_static_3600 == 1 } { HTTP::header replace "Cache-Control" "max-age=3600" HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+3600)] -format "%a, %d %h %Y %T GMT" -gmt true]" } elseif { $expire_static_86400 == 1 } { HTTP::header replace "Cache-Control" "max-age=86400" HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+86400)] -format "%a, %d %h %Y %T GMT" -gmt true]" } elseif { $expire_static_604800 == 1 } { HTTP::header replace "Cache-Control" "max-age=604800" HTTP::header replace "Expires" "[clock format [expr ([clock seconds]+604800)] -format "%a, %d %h %Y %T GMT" -gmt true]" } else { HTTP::header replace Expires -1 } }
- hoolio
Cirrostratus
Hi Rodrigo, - hoolio
Cirrostratus
Using getfield to split the path wouldn't work if there is a period anywhere else in the path (like /path/to/my.image.jpg).class class_cacheable_file_types { "htm 300" "html 300" "css 3600" "js 3600" "bmp 86400" "gif 86400" "jpeg 86400" "jpg 86400" "png 86400" "pdf 604800" "swf 604800" "ico 604800" }
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 [findclass [getfield [string tolower [HTTP::uri]] "." 2] 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} } } }
class class_cacheable_file_types { ".htm 300" ".html 300" ".css 3600" ".js 3600" ".bmp 86400" ".gif 86400" ".jpeg 86400" ".jpg 86400" ".png 86400" ".pdf 604800" ".swf 604800" ".ico 604800" }
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 [findclass [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} } } }
- Colin_Walker_12Historic F5 AccountDefinitely a cool example. Feel free to email it in to me and I can get it posted up to the codeshare (or with your permission I'll just grab it from here) as the upload form is undergoing some work right now.
- Nicolas_Menant
Employee
hi,
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects