Using Datascript to Redirect Traffic Based on HTTP Referer Header to certain pool.
search cancel

Using Datascript to Redirect Traffic Based on HTTP Referer Header to certain pool.

book

Article ID: 412178

calendar_today

Updated On:

Products

VMware Avi Load Balancer

Issue/Introduction

  • When handling HTTP requests, the Referer header can be used to identify the source of the request and ensure traffic is coming from a trusted domain.
  • In some cases, using an Avi HTTP policy alone cannot fully achieve the required redirection logic. In such scenarios, a Datascript can be applied to implement fine-grained traffic routing based on the request URI and Referer header.

Resolution

  • You can use a Datascript and attach it to the Virtual Service under:
    VS → Policies → Datascript → HTTP Request Event.

 

  • Example Use Case:
  • If the request URI ends with LoginDay.path

    • And the Referer header also ends with 5LoginDay.path → Redirect traffic to XFF Pool

    • Else → Redirect traffic to the Default Pool

 

Sample Datascript:

 

if path:sub(-string.len("LoginDay.path")) == "LoginDay.path" then
    if referer:sub(-string.len("5LoginDay.path")) == "5LoginDay.path" then
        avi.http.add_header("X-Debug", "REQ: LoginDay.path + Referer 5LoginDay.path → pool XFF")
        avi.pool.select("XFF")
        return
    else
        avi.http.add_header("X-Debug", "REQ: LoginDay.path + other Referer → pool Default")
        avi.pool.select("Default")
        return
    end
end