Forum Discussion

N_67263's avatar
N_67263
Icon for Nimbostratus rankNimbostratus
Aug 28, 2017
Solved

iRule which strips of the domain name and replaces that with another domain name.

Team, Can anyone help me with an iRule which strips of the domain name and does a DNS query with some other domain name? e.g. The F5 intercepts a request on "xyz.externaldomain.com". F5 strips of the externaldomain.com and replaces this with "xyz.internaldomain.com" and does a lookup. The IP address returned in the lookup is used for the node in the VIP.

 

Thanks!! N.

 

  • Hi,

    Maybe the following irule may help you :

    when HTTP_REQUEST {
        if { [HTTP::host] contains "xyz.externaldomain.com" } {
            HTTP::header replace Host "xyz.internaldomain.com"
        }
    }
    

    You may also use

    string map
    command to make the replacement more dynamic

21 Replies

  • Hi,

    Maybe the following irule may help you :

    when HTTP_REQUEST {
        if { [HTTP::host] contains "xyz.externaldomain.com" } {
            HTTP::header replace Host "xyz.internaldomain.com"
        }
    }
    

    You may also use

    string map
    command to make the replacement more dynamic

    • Yann_Desmarest's avatar
      Yann_Desmarest
      Icon for Cirrus rankCirrus

      Using string map :

      when HTTP_REQUEST {
          if { [HTTP::host] contains "xyz.externaldomain.com" } {
              set new_host [string map {externaldomain internaldomain} [HTTP::host]]
              HTTP::header replace Host $new_host
          }
      }
      
    • N_67263's avatar
      N_67263
      Icon for Nimbostratus rankNimbostratus

      hey Yann....thanks a ton. I think the logic should work. However, we would like to use the new domain to do a lookup and what ever is the IP returned use that IP address in the node to forward the traffic.

       

      Let me work on some extension of iRule with the logic you provided

       

    • N_67263's avatar
      N_67263
      Icon for Nimbostratus rankNimbostratus

      This is the iRule that I have mapped out, still does not work. :( any comments?

       

      when HTTP_REQUEST { if { [HTTP::host] contains "externaldomain.com" } { set new_host [string map {externaldomain internaldomain} [HTTP::host]] set ips [lindex [RESOLV::lookup -a [$new_host]] 0] node $ips [TCP::local_port] } }

       

  • Hi,

    Maybe the following irule may help you :

    when HTTP_REQUEST {
        if { [HTTP::host] contains "xyz.externaldomain.com" } {
            HTTP::header replace Host "xyz.internaldomain.com"
        }
    }
    

    You may also use

    string map
    command to make the replacement more dynamic

    • Yann_Desmarest_'s avatar
      Yann_Desmarest_
      Icon for Nacreous rankNacreous

      Using string map :

      when HTTP_REQUEST {
          if { [HTTP::host] contains "xyz.externaldomain.com" } {
              set new_host [string map {externaldomain internaldomain} [HTTP::host]]
              HTTP::header replace Host $new_host
          }
      }
      
    • N_67263's avatar
      N_67263
      Icon for Nimbostratus rankNimbostratus

      hey Yann....thanks a ton. I think the logic should work. However, we would like to use the new domain to do a lookup and what ever is the IP returned use that IP address in the node to forward the traffic.

       

      Let me work on some extension of iRule with the logic you provided

       

    • N_67263's avatar
      N_67263
      Icon for Nimbostratus rankNimbostratus

      This is the iRule that I have mapped out, still does not work. :( any comments?

       

      when HTTP_REQUEST { if { [HTTP::host] contains "externaldomain.com" } { set new_host [string map {externaldomain internaldomain} [HTTP::host]] set ips [lindex [RESOLV::lookup -a [$new_host]] 0] node $ips [TCP::local_port] } }

       

  • Hi N,

     

    please don't open multiple question for the same topic. It doesn't make sense if independent people spend their valuable time to answer your redundant questions over and over...

     

    https://devcentral.f5.com/questions/understanding-the-host-in-the-http-request-55264

     

    https://devcentral.f5.com/questions/irule-which-strips-of-the-domain-name-and-replaces-that-with-another-domain-name-55273

     

    https://devcentral.f5.com/questions/node-autoselection-using-dns-55263

     

    Note: I've already told you to add some error handles to your iRule, to become able to indentify where it breaks. In addition to that I've linked K12225 [click me] to make sure the DNS on your F5 allows recursive DNS queries. I've also speak out the recommendation to setup a DNS Virtual Server to become able to query a pool of redundant DNS servers.

     

    Well, based on your latest requirements (added external / internal DNS domain conversation) the resulting iRule code would look like that...

     

    when HTTP_REQUEST { 
        if { [HTTP::host] eq "xyz.externaldomain.com" } then {
            set ips [lindex [RESOLV::lookup -a "[getfield [HTTP::host] "." 1].internaldomain.com"] 0]
            log local0.debug "Debug: Resolved address for \"[getfield [HTTP::host] "." 1].internaldomain.com\" = \"$ips\"" 
            if { $ips ne "" } then {
                node $ips [TCP::local_port]
            } else {
                HTTP::respond 504 content "Bad Request - unknown HOST value"
            }
        } else {
            HTTP::respond 504 content "Bad Request - wrong HOST value"
        }
    }

    Cheers, Kai

     

  • Hi,

    You can define which dns server is used for DNS requests.

    If you want to manage DNS servers, create a LTM pool including dns servers with dns monitor and use this irule. the dns pool name in this irule is

    p_dns

    when CLIENT_ACCEPTED {
            foreach dns [active_members -list p_dns] {   
             Check if the first list element was empty
            if {[set dest [lindex [RESOLV::lookup @[lindex $dns 0] -a "www.abc.com";] 0]] ne ""} {
                 Set Node IP based on DNS resolution
                node $dest 443
                break
            }
        }
    }