Forum Discussion

Fereda_187281's avatar
Fereda_187281
Icon for Nimbostratus rankNimbostratus
Dec 14, 2015

Irule-Replace URL with random node specific and Port

Hi all;

 

I want to do that when client a request url : than irule replace specific port). Another client request same url: than irule replace specific port).

 

So my specific irule could replace pattern vip-ip and port with node's IP and node's port.My pool have more than one member.

 

Please help me how can write irule.

 

Thanks.

 

Ferhat De

 

4 Replies

  • Hi,

    a HTTP request received by LTM is:

    GET /foo/ HTTP/1.1
    Host: abc.com:port (if port is not default one)
    User-Agent: user agent string
    other headers...
    

    the default behavior of LTM when load balancing requests is to change both destination IP and port without changing anything in the request.

    Do you want to rewrite Host header with the pool member ip address and port instead of VS fqdn and port?

  • Hi Ferhat,

    as Stanislas already explained your LTM would already take care of the load balancing decission, based on available pool members and the configured load balancing lalgorythm. So no need to use iRules for that...

    To change the HOST headers value to reflect the selected node IP and also port number you may use this rather simple iRule...

    when HTTP_REQUEST_SEND {
        HTTP::host "[IP::server_addr]:[TCP::server_port]"
    }
    

    ... or if using multiple route domains...

    when HTTP_REQUEST_SEND {
        HTTP::host "[getfield [IP::server_addr] "%" 1]:[TCP::server_port]"
    }
    

    The outlined iRule will be triggered on each HTTP request right before sending the request to the real server and simply changes to HOST header value to the IP and Port of the selected server.

    Cheers, Kai

  • Hi,

    I would apply the header insertion in the context of LB_SELECTED.

    The iRule below shows another option to insert real hostname to be retrieved from an array:
    when RULE_INIT {
        array set static::node_hostname {
            10.131.131.63 host1.lb-net.bit
            10.131.131.64 host2.lb-net.bit
        }
    }
    when LB_SELECTED {
        if {[HTTP::header exists Host]} {
             HTTP::header replace Host [getfield [array get static::node_hostname [LB::server addr]] " " 2]
            HTTP::header replace Host "[LB::server addr]:[LB::server port]"
        } else {
            HTTP::header insert Host "[LB::server addr]:[LB::server port]"
        }
    }
    

    Be aware, that servers may send absolute redirects containing the hostname they received in the clients request which may not be resolved by the client.

    So perhaps you will need some additional iRule logic to rewrite redirects and the payload containing references.

    Thanks, Stephan