Content Switching with the NetScaler

This blog post will look at a feature on the Citrix NetScaler called Content Switching (CS). What CS allows you to redirect traffic based on criteria you set; it can be a URL string, HTTP request, etc. This is good if you have a site with dynamic content like ASP pages, pictures, and/or videos. You can dynamically route traffic to one VIP and static traffic to another VIP. Another use is if you have a read-only and a read-write database setup.

Read requests can go to the read farm, and write requests can go to the write farm. This post will look at whether you have a mobile farm and a non-mobile farm; mobile requests will be directed to the mobile farm and vice versa. Our Topology is the same: three servers, but the mobile site listens on a different port, which will be reflected in the service configuration. For a refresher, the topology we are using is below;

The criteria to forward to the mobile site are to look in the request header for User-Agent Mobile-Browser. The sites should have the following configured for them;

  • Monitor – built-in HTTP check
  • Persistence – None
  • Loadbalancing – round-robin

We will do this from the command line as with the previous posts. The first thing we have to do is define the servers, service, and VIP. A point to note: if you are creating a CS VIP, it does not get an IP address. It will use the IP address of the CS VIP.

Configuration

Virtual_NS> add server BLUE-SERVER 192.168.99.25
Virtual_NS> add server RED-SERVER 192.168.99.26
Virtual_NS> add server PURPLE-SERVER 192.168.99.27
Virtual_NS> add service BLUE-MAIN BLUE-SERVER HTTP 80
Virtual_NS> add service RED-MAIN RED-SERVER HTTP 80
Virtual_NS> add service PURPLE-MAIN PURPLE-SERVER HTTP 80
Virtual_NS> add service BLUE-MOBILE BLUE-SERVER HTTP 695
Virtual_NS> add service RED-MOBILE RED-SERVER HTTP 695
Virtual_NS> add service PURPLE-MOBILE PURPLE-SERVER HTTP 695
Virtual_NS> add lb vserver MAIN_SITE HTTP -lbMethod ROUNDROBIN
Virtual_NS> add lb vserver MOBILE_SITE HTTP -lbMethod ROUNDROBIN
Virtual_NS> bind lb vserver MOBILE_SITE BLUE-MOBILE
Virtual_NS> bind lb vserver MOBILE_SITE RED-MOBILE
Virtual_NS> bind lb vserver MOBILE_SITE PURPLE-MOBILE
Virtual_NS> bind lb vserver MAIN_SITE BLUE-MAIN
Virtual_NS> bind lb vserver MAIN_SITE PURPLE-MAIN
Virtual_NS> bind lb vserver MAIN_SITE RED-MAIN
Virtual_NS> bind service RED-MAIN monitorName http
Virtual_NS> bind service BLUE-MAIN monitorName http
Virtual_NS> bind service PURPLE-MAIN monitorName http
Virtual_NS> bind service RED-MOBILE monitorName http
Virtual_NS> bind service BLUE-MOBILE monitorName http
Virtual_NS> bind service PURPLE-MOBILE monitorName http

Let us look at content switching. Is it enabled?

Virtual_NS> show ns feature | grep "CS"
4) Content Switching CS OFF

OK, so the feature is turned off. We must ensure we are licensed to run it; if yes, we can enable it.

Virtual_NS> show ns license | grep "Content Switching"
Content Switching: YES

So we are; let us enable it.

Virtual_NS> enable ns feature CS
Virtual_NS> show ns feature | grep "CS"
4) Content Switching CS ON

The first step is to create the policy we will work with. The NetScaler has an intuitive GUI for creating these policies, and once you get the hang of them, it will become second nature for you to create them via the CLI. So we are looking in the HTTP REQ packet header for User-Agent Mobile-Browser.

Virtual_NS> add cs policy MOBILE-1 -rule "HTTP.REQ.HEADER(\"User-Agent\").CONTAINS(\"Mobile-Browser\")"

Now, we create the CS VIP.

Virtual_NS> add cs vserver CS-VIP1 HTTP 192.168.99.50 80

Finally, we bind the policy to the LB VServers

Virtual_NS> bind cs vserver CS-VIP1 -policyName MOBILE-1 -targetLBVserver MOBILE_SITE -priority 25
Virtual_NS> bind cs vserver CS-VIP1 -lbvserver MAIN_SITE

Verification

Now we verify! We will use CURL to test access to the site passing in the fake user agent and no user agent to see what happens.

CURL with no user agent

Mike@Mikes-MacBook-Pro:~$ curl https://192.168.99.50

The response from the curl:

<html>
<title>Purple Server</title>
<body style="background-color:5F0E61">
<center>
<h1><font color="ffffff"><b><i> Welcome to the Purple Server!</i></b</h1>
</center>
</body>
</html>

CURL with User-Agent

Mike@Mikes-MacBook-Pro:~$ curl https://192.168.99.50 -A Mobile-Browser

Response:

<html>
<title>Red Server</title>
<body style="background-color:red">
<center
<h1><font color="ffffff"><b><i> Welcome to the Red Server on a mobile site!</i></b</h1>
</body>
</html>

CURL with the matching user agent in between other user agents

Mike@Mikes-MacBook-Pro:~$ curl https://192.168.99.50 -A "Safari-Gecko Mobile-Browser Accel 10"

Response:

<html>
<title>Red Server</title>
<body style="background-color:red">
<center
<h1><font color="ffffff"><b><i> Welcome to the Red Server on a mobile site!</i></b</h1>
</body>
</html>

Now, some verification commands (specific to CS);

  • Show cs vserver (Name): shows information related to the CS-vserver. It shows what policies are bound, how many hits each policy has, and what lb-vserver the policy points to
  • Show cs policy (Name): this will show all the bound and unbound policies and what they do