Forum Discussion
Tokyo_Jihen_931
Nimbostratus
Dec 03, 2010iRule for URL persistence
Hello all,
I am a newbie of iRules programming. My system is BIG-IP 9.1.2. I am trying to implement persistence using a string that appear in URL.
For example.
http://test.big-ip.com/install/123456
http://test.big-ip.com/scripts/456788
I want the system when seeing the identifier (e.g. 123456, 456788) in the URL, it will use that identifier to persist my traffic to my nodes.
The identifier is always contains 6 characters and should be after the string "install/" or "scripts/"
Does anyone have similar environment with me? Or can provide me any iRule sample to do this?
Thanks
12 Replies
- Chris_Miller
Altostratus
Basically, if the URI contains "123456," go to server 1 and if it contains "456788," goes to server 2? - hoolio
Cirrostratus
Maybe something like this?when HTTP_REQUEST { Check for /install/ or /scripts/ in the path switch -glob [HTTP::path] { "*/install/*" { Parse the URI "directory" after install. Look for /install/, skip 9 characters and match up to the next /. set session_id [findstr [HTTP::path] /install/ 9 /] } "*/scripts/*" { Parse the URI "directory" after scripts. Look for /scripts/, skip 9 characters and match up to the next /. set session_id [findstr [HTTP::path] /scripts/ 9 /] } default { set session_id "" } } if {$session_id ne ""}{ Persist on the parsed session ID for X seconds persist uie $session_id 1800 } }
Aaron - hoolio
Cirrostratus
Actually, this might not work. With the above approach, the client could get load balanced to a different server than the one that generated the session. To avoid this scenario, you'd need to parse the persistence token in the response and then persist on it in subsequent requests.
Does the application include the session ID in a redirect or other HTTP response header? If so, you could parse the session ID relatively easily. Else, you'd need to look through the response content. For the latter approach, there's a codeshare example:
Persist client on response content with stream
http://devcentral.f5.com/wiki/default.aspx/iRules/Persist_client_on_response_content_with_stream
If you can clarify whether this is a problem and if so where the session ID can be found when it's initially created, we can provide you with more suggestions.
Aaron - Tokyo_Jihen_931
Nimbostratus
Thank you very much Aaron.
But in my enviroment, the 6 characters string is the identifier.
http://test.big-ip.com/install/123456/whateverxxxxxx
http://test.big-ip.com/scripts/456788/whateverxxxxx
It is as Mr. Chris Miller said, "if the URI contains "123456," go to server 1 and if it contains "456788," goes to server 2"
So I want to know how i can "grep" the 6 characters string from the url and then set it as "session_id" and then "persist" the connection.
I don't have any idea for the iRule Syntax for doing that, so please advise me. Thanks a lot~ - Chris_Miller
Altostratus
You don't necessarily need to set it as a sessionid, you should simply be able to send the request to server x.
I assume switch -glob would be the most efficient way to go, but someone can correct me.when HTTP_REQUEST { switch -glob [HTTP::uri] { "*123456*" { pool poolname member x.x.x.x y } "*456788*" { pool poolname member x.x.x.x y } }}
In the above, poolname = the name of your pool. x.x.x.x = the ip address of the server and y = the port on which the pool member is listening.
Aaron - this look good? - Colin_Walker_12Historic F5 AccountThat should work fine, Chris. There's no need for persistence technically, if you're going to use the iRule for redirection every time anyway. You could of course set persistence within the switch, but that's only if you want to be able to get the user back to the same server without the use of the iRule.
Colin - Tokyo_Jihen_931
Nimbostratus
Thank you very much Chris~~
Actually the 6 characters string is a random number that is generated from the web client. (Once the random number is generated, that number will be the same during the transactions) So i need to use that string as the identifier to persist the connection.
http://test.big-ip.com/install/123456/whateverxxxxxx
In my case, the 5th field of the url is the 6 characters string. (is that right?)
So to get the 5th field in the url, i used [getfield [HTTP::uri] "/" 5] .
when HTTP_REQUEST {
set session_id [getfield [HTTP::uri] "/" 5]
if {$session_id ne ""} {
persist uie $session_id 1800
}
}
I tried the above iRule in my F5 but it seems not work, the connection cannot be persist.
any thing wrong in my iRule ?
Thank you for your help. - hoolio
Cirrostratus
I think you'd need to parse the random token from the response and create a persistence record then. Else, just by just looking at the request, the client would be load balanced again and then on that response LTM would add a persistence record. If the random token is only found in the response headers you could use HTTP::header $header_name to parse it and then add a persistence record with the persist add command. Else, if the token is in the response payload, you could use a stream profile and iRule like this:
Persist client on response content with stream
http://devcentral.f5.com/wiki/default.aspx/iRules/Persist_client_on_response_content_with_stream
A much simpler option would be to persist all requests using cookie insert persistence. This could be done without an iRule.
Aaron - Chris_Miller
Altostratus
So, it's not the "123456" that determines which server to go to, but the "xxxxxxx" part?
I like Aaron's idea of persisting based on the response...that's very similar to how I handle persistence for my site. - yxshybj_101037
Nimbostratus
I have a environment , can't use insert client ip in the http head , but the device(is not the end client ) in front of f5 coult insert the client ip in the url in the follow format ,:
http://test.big-ip.com/ebv/rbv&clientIP=192.168.1.5?svk
Does the follow irules "
when HTTP_REQUEST {
set session_id [getfield [HTTP::uri] "/" 11]
if {$session_id ne ""} {
persist uie $session_id 1800
}
}
"
work ?
Does anyone have similar environment with me? Or can provide me any iRule sample to do this?
thanks !
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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