Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology

Dynamic port selection not working

Hello All. I'm trying to compose an irule that will direct the traffic to a dynamically chosen port in a pool, according to the URL the user uses. After much searching I got to the point where the node and the port are correctly selected, but the NLB disregards the node command and directs the traffic to the original port.

The URL is made of 3 letters of the service and 3 digits of the wanted inside-component. Together they compose the destination port. The user uses HTTPS(443), but the NLB has to direct the traffic to the "member:composed-port" according to the URL.

The VIP has address and port translation enabled. To be sure of that I included those commands in the irule. The member in the pool is defined with "port=all services".

 when RULE_INIT {
    # 0 = none, 1 = debug, 2 = verbose
    set static::APsp_Debug 2
      translate address enable
      translate port enable
 when HTTP_REQUEST priority 1 {
    # Extract the last 3 chars from the hostname (e.g. 200 from ADM200.company.com)
    set APsp_inside_code [string range [getfield [HTTP::host] "." 1] end-2 end]
    #  Extract the first 3 chars from the hostname (e.g. ADM from ADM200.company.com)
    set APsp_service_code [string range [getfield [HTTP::host] "." 1] 0 2 ]

    switch -glob [string tolower $APsp_service_code]
        { "adm" {set APsp_dest_port "60$APsp_inside_code" }
          "rst" {set APsp_dest_port "64$APsp_inside_code" }
           default { log local0.error "service code not found. [HTTP::host][HTTP::uri]"
             HTTP::respond 404 "Not Found" 
 when LB_SELECTED priority 1 {
    set APsp_dest_node [LB::server addr]
    # replace the host header so the server will think that this is the original request 
    HTTP::header replace Host "company.co.il"
    # go to load balanced member, but with the needed port

         if {$static::APsp_Debug > 0} {
             log local0.info "LBserver= [LB::server addr] node=$APsp_dest_node port=$APsp_dest_port"
         node $APsp_dest_node:$APsp_dest_port
         log local0.info "after node command LBserver= [LB::server]"
 when LB_FAILED { 
   log local0.error "Selected server $APsp_dest_node:$APsp_dest_port is not responding" 
   HTTP::respond 404 "Not Found" 
    if {$static::APsp_Debug > 0} {
             log local0.info "serverport: [TCP::server_port]"


Here are the Debug messages:

 Rule /Common/Event_Logger <HTTP_REQUEST>: Client requested http(s)://adm200.company.com/appbuilder/forms?code=8.
 Rule /Common/Event_Logger <HTTP_REQUEST>: Client request DIDN'T match any policy rule.
 Rule /Common/MY_select_port <LB_SELECTED>: LBserver= node= port=60200
 Rule /Common/MY_select_port <LB_SELECTED>: after node command LBserver= 60200
 Rule /Common/Event_Logger <LB_SELECTED>: Client farwarded to 60200 /appbuilder/forms?code=8.
 Rule /Common/Event_Logger <SERVER_CONNECTED>: Client connected from to node
 Rule /Common/MY_select_port <SERVER_CONNECTED>: serverport: 443
 Rule /Common/Event_Logger <HTTP_REQUEST_SEND>: Client sending request to
 Rule /Common/Event_Logger <HTTP_REQUEST_RELEASE>: Client releasing request to
 Rule /Common/Event_Logger <HTTP_RESPONSE>: Client got a response from
 Rule /Common/Event_Logger <HTTP_RESPONSE_RELEASE>: Client 404 response released from
 Rule /Common/Event_Logger <SERVER_CLOSED>: Connection from to Server has closed.

As you can see, the node command did the correct selection but the server connect went on with port 443.

The pool definition:

   ltm pool /Common/service_pool {
       load-balancing-mode observed-member
       members {
           /Common/ {
           /Common/ {
       monitor /Common/gateway_icmp

Thanks in advance. Gil.

Rate this Question

Answers to this Question


Hi Gil, LB_SELECTED event is already after the node (and port) selection. Try the following command instead of the "node":

LB::reselect node [LB::server addr] $APsp_dest_port
Comments on this Answer
Comment made 1 week ago by gilbs 11

Thanks. the LB::reselect did the trick.