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

It’s not ideal but if you configure multiple Pools accordingly, with the Pool name ending in the port number it supports then the following pretty short rule should work as long as a four digit port number is always used. It is probably worth adding in some checks to ensure you have a valid value before rewriting the URI and Host and specifying the Pool.

when HTTP_REQUEST {
 Find the first / in the URI, skip 4 characters and match the rest of the string, set this as variable newuri
 set newuri [findstr [HTTP::uri] "/" "4"]
 Find the first / in the URI, skip 0 characters and match until the next /, this this as variable newport
 set newport [findstr [HTTP::uri] "/" "0" "/"]
 Change the URI before we send this request to a real server
 HTTP::uri "$newuri"
 Change the HTTP Host header
 HTTP::header replace Host "[HTTP::host]:$newport"
 Use the relevant Pool
 pool pool_$newport
}

Thank you Steve! So no way to use one pool and just go to different ports right? I am a bit lazy but I can just create a bunch of pools up front. Or write a script that creates them with the right naming convention and just run it when I get requests for more.

Thanks

Joe

You could setup a wildcard Pool (I think) but you can’t specify a Pool and port in an iRule, only a Pool or a Pool Member and port which doesn’t really meet your requirements unfortunately.

A wildcard pool suggest no port translation at the VIP, but then you probably are translating the port number (from port 80/443 to something else internal). Your BEST bet, if you have control over the total number of ports/pools is to create a pool for each. In this way you have better control over load balancing and monitors. The alternative is to use a node command, which brings with it the complexity of some manual load balancing and persistence work.

Not to look a gift horse in the mouth but I can’t get this script to work.

the only modification I made was to change the name of the pools to chuckpool_8080

but this is the error I get

TCL error: /Common/test-portredirect - no such pool: chuckpool_ (line 10) invoked from within “pool chuckpool_$newport”

Do you get this error at runtime or just when saving the rule?

At runtime. The rule saves fine and when I put it in it is like it isn’t getting the value from the variable for newport?

OK, thought so. I’ve added some logging and a Variable check to help troubleshoot;

when HTTP_REQUEST {
 set newuri [findstr [HTTP::uri] "/" "4"]
 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"
 }
}

OK so I changed the url to .test.com the rest is the same. The only thing I changed in your script above is the line pool pool_$newport

my pools are chuckpool_number the rest I left. It is showing the port as 0, 1, 2, 4 I

tried chucktest.test.com/8080/ESB/App1

chucktest.test.com/8081/ESB/App2

chucktest.test.com/8082/ESB/App3

Then just for giggles I tried

chucktest.test.com/8084/ESB/App3 which has no pool

What I am trying to get is

chucktest.test.com/8080/ESB/App1 to convert to chucktest.insidehost:8080/ESB/App1

I have three pools setup

chucktest_8080

chucktest_8081

chucktest_8082

And would like it to take the /8080 and convert it to the pool it is being sent to

Below are the errors:

Mon Apr 22 14:33:54 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::

Mon Apr 22 14:33:54 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty

Mon Apr 22 14:33:54 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:

Mon Apr 22 14:33:54 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: 4/ESB/App3

Mon Apr 22 14:33:46 EDT 2013 info tmm1 tmm1[8660] Rule /Common/log-test : chucktest.test.com::

Mon Apr 22 14:33:46 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : Newport Variable was empty

Mon Apr 22 14:33:46 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New Port is:

Mon Apr 22 14:33:46 EDT 2013 info tmm1 tmm1[8660] Rule /Common/jose-portredirect-test : New URI is: 2/ESB/App3

Mon Apr 22 14:33:33 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::

Mon Apr 22 14:33:33 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty

Mon Apr 22 14:33:33 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:

Mon Apr 22 14:33:33 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: 1/ESB/App2

Mon Apr 22 14:33:23 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::

Mon Apr 22 14:33:23 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty

Mon Apr 22 14:33:23 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:

Mon Apr 22 14:33:23 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: 0/ESB/App1

Mon Apr 22 14:30:28 EDT 2013 info tmm tmm[8659] Rule /Common/log-test : chucktest.test.com::

Mon Apr 22 14:30:28 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : Newport Variable was empty

Mon Apr 22 14:30:28 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New Port is:

Mon Apr 22 14:30:28 EDT 2013 info tmm tmm[8659] Rule /Common/jose-portredirect-test : New URI is: 0/ESB/App1

Thank you

Joe

:I wonder if this is because we’re relating to the Host rather than the URI. Is an absolute path being used in the request? Can you log local0. "[HTTP:uri] and confirm? Also, perhaps this should be 5; set newuri [findstr [HTTP::uri] “/” “4”]

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

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"
 }
}

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

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"
 }
}

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/

[redacted]

Sweet. I think we can just change this;

[findstr [HTTP::uri] "/" 1 5]

This might be better (if it works);

[findstr [HTTP::uri] "/" 0 4]

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