Forum Discussion
irule to pick a number out of a URI and make it the port
I have developers who want to tie everything off the same URL and have it go to different ports on the same group of hosts. If you have been following my posts, it is almost the exact opposite problem I had before but hey that's what keeps it interesting right?
So I want to write an Irule that takes URLs like this
joe.test.com/8050/someapp/someoption
joe.test.com/8060/otherapp/differentoption
and convert them to
insidepool:8050/someapp/someoption
insidepool:8060/otherapp/differentoption
I would like it to just be a general take the number make it a variable and then put it in the same URI as a port. Or even if I can just say take everything after my original URL joe.test.com and put it after the inside vip with a colon for port.
Or do I have to create a pool for each and port and then do an if then else kind of thing? What I would like to do if I can is keep it from being a long rule where everything has to go through this long 20 if then statement to find the right pool.
Anyone do anything like this? If so how do you setup the pool? 1.1.1.1:* ??
Thank you in advance for your help!
Joe
20 Replies
- sundogbrew
Altocumulus
Steve,
Thanks for your help!
If I get behind the firewall and F5 I can go to
http://172.20.5.15:8080/ESB/App1/
I have my pool set for 8080 and 172.20.5.15 is the only host in my pool.
Tue Apr 23 12:04:35 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: /ESB/App1/
Tue Apr 23 12:04:35 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:
Tue Apr 23 12:04:35 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty
Tue Apr 23 12:04:35 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : /ESB/App1/
Tue Apr 23 12:04:35 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::
Tue Apr 23 12:04:49 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: /ESB/App2/
Tue Apr 23 12:04:49 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:
Tue Apr 23 12:04:49 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty
Tue Apr 23 12:04:49 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : /ESB/App2/
Tue Apr 23 12:04:49 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::
Tue Apr 23 12:05:00 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: /ESB/App3/
Tue Apr 23 12:05:00 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:
Tue Apr 23 12:05:00 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty
Tue Apr 23 12:05:00 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : /ESB/App3/
Tue Apr 23 12:05:00 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::
It looks like it works with 5 but it isn't doing the port, it is only working for the default pool. So I think it is picking up the right URI and just going to the default pool which is 8080. Not working in the App2 and App3 because of the port? I don't have to define the pools in the rule do I? Can the rule push traffic to a pool that isn't in the config for the Vhost?
Thanks
Joe - What_Lies_Bene1
Cirrostratus
Hmmm, I think I'm missing something here. Can you try this - just an extra log statement to record the original URI etc.when HTTP_REQUEST { set newuri [findstr [HTTP::uri] "/" "4"] log local0. "Original request is: [HTTP::host][HTTP::uri]" log local0. "New URI is: $newuri" set newport [findstr [HTTP::uri] "/" "0" "/"] HTTP::uri "$newuri" log local0. "New Port is: $newport" HTTP::header replace Host "[HTTP::host]:$newport" if { $newport ne "" } { Use the relevant Pool pool pool_$newport } else { log local0. "Newport Variable was empty" } } - sundogbrew
Altocumulus
Steve, I had to change your code to be 5 as that seems to work but here is the messages I get
Tue Apr 23 13:50:46 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Original request is: chucktest.test.com/8080/ESB/App1/
Tue Apr 23 13:50:46 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: /ESB/App1/
Tue Apr 23 13:50:46 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:
Tue Apr 23 13:50:46 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty
Tue Apr 23 13:50:46 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::
Tue Apr 23 13:51:03 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Original request is: chucktest.test.com/8081/ESB/App2/
Tue Apr 23 13:51:03 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: /ESB/App2/
Tue Apr 23 13:51:03 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:
Tue Apr 23 13:51:03 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty
Tue Apr 23 13:51:03 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::
It is not populating the port value that is where I am stumped? The only reason the 8080 works is because that is the default pool.
Thanks
Joe - What_Lies_Bene1
Cirrostratus
Odd. OK, how about this (I've removed some double quotes and used a length rather than / for the terminator with the second findstr command);when HTTP_REQUEST { set newuri [findstr [HTTP::uri] "/" 5] log local0. "Original request is: [HTTP::host][HTTP::uri]" log local0. "New URI is: $newuri" set newport [findstr [HTTP::uri] "/" 0 4] HTTP::uri "$newuri" log local0. "New Port is: $newport" HTTP::header replace Host "[HTTP::host]:$newport" if { $newport ne "" } { Use the relevant Pool pool pool_$newport } else { log local0. "Newport Variable was empty" } } - sundogbrew
Altocumulus
OH MAN, we are so close!!!
I was getting /808 so I changed set newport [findstr [HTTP::uri] "/" 0 4] to set newport [findstr [HTTP::uri] "/" 0 5] and am now getting the port but need to lose the /
how would I do that?
Tue Apr 23 15:44:09 EDT 2013 err tmm1 tmm1[8660] 01220001 TCL error: /Common/jose-portredirect-test - no such pool: chuckpool_/8080 (line 11) invoked from within "pool chuckpool_$newport "
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New Port is: /8080
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New URI is: /ESB/App1/
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : Original request is: chucktest.test.com/8080/ESB/App1/
Tue Apr 23 15:44:09 EDT 2013 err tmm1 tmm1[8660] 01220001 TCL error: /Common/jose-portredirect-test - no such pool: chuckpool_/8080 (line 11) invoked from within "pool chuckpool_$newport "
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New Port is: /8080
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New URI is: /ESB/App1/
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : Original request is: chucktest.test.com/8080/ESB/App1/
Tue Apr 23 15:44:09 EDT 2013 err tmm1 tmm1[8660] 01220001 TCL error: /Common/jose-portredirect-test - no such pool: chuckpool_/8080 (line 1) invoked from within "pool chuckpool_$newport "
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New Port is: /8080
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New URI is: /ESB/App1/
Tue Apr 23 15:44:09 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : Original request is: chucktest.test.com/8080/ESB/App1/ - What_Lies_Bene1
Cirrostratus
[redacted]
- What_Lies_Bene1
Cirrostratus
Sweet. I think we can just change this;[findstr [HTTP::uri] "/" 1 5] - What_Lies_Bene1
Cirrostratus
This might be better (if it works);[findstr [HTTP::uri] "/" 0 4] - sundogbrew
Altocumulus
Steve, you SO ROCK!!! Thank you so much for your help!
I had to change the 5 back to a 4 once I added the 1. It also works with or without the / in "" I will investigate that more.
the only strange thing I am seeing now is this line
Tue Apr 23 15:58:24 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com:8082:8082
not sure why the port is showing up twice or if it matters? It works and that is what REALLY matters!!!!
Here is the final code if anyone wants to use it
when HTTP_REQUEST {
set newuri [findstr [HTTP::uri] "/" 5]
log local0. "Original request is: [HTTP::host][HTTP::uri]"
log local0. "New URI is: $newuri"
set newport [findstr [HTTP::uri] "/" 1 4]
HTTP::uri "$newuri"
log local0. "New Port is: $newport"
HTTP::header replace Host "[HTTP::host]:$newport"
if { $newport ne "" } {
Use the relevant Pool
pool chuckpool_$newport }
else {
log local0. "Newport Variable was empty"
}
}
I set this up with pools named chuckpool_8080, chuckpool_8081... and then I tell the developers to just use the port they want in the URI as in newapp.test.com/8080/app/and/more/crap the script pulls out the port and sends it to the pool chuckpool_8080 (or whatever they choose) and I just use one of the pools as the default. It puts the URI back on and you get to the pool you want with the URI intact.
Thank you again SO MUCH!
Joe - What_Lies_Bene1
Cirrostratus
Glad we finally got there, thanks.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* 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
