Advanced

Special use cases

Advanced patterns: load balancing, geo-routing, and path-based routing.

Load balancing by client IP last octet

Deterministically split traffic between two proxies based on the client machine's IP address. Even-numbered last octet → proxy A; odd → proxy B. Each client is always sticky to the same proxy.

proxy.pac
function FindProxyForURL(url, host) {
  if (isPlainHostName(host)) { return "DIRECT"; }

  var myip      = myIpAddress();          // e.g. "192.168.1.24"
  var parts     = myip.split(".");
  var lastOctet = parseInt(parts[3], 10);

  if ((lastOctet % 2) === 0) {
    return "PROXY proxy-a.corp.com:8080; PROXY proxy-b.corp.com:8080";
  } else {
    return "PROXY proxy-b.corp.com:8080; PROXY proxy-a.corp.com:8080";
  }
}

Load balancing by hostname hash

Distribute requests across N proxies based on a hash of the target hostname. The same target host always routes to the same proxy — useful for connection reuse and cache warming.

proxy.pac
function FindProxyForURL(url, host) {
  if (isPlainHostName(host)) { return "DIRECT"; }

  var proxies = [
    "PROXY proxy-0.corp.com:8080",
    "PROXY proxy-1.corp.com:8080",
    "PROXY proxy-2.corp.com:8080"
  ];

  // Simple hash: sum of char codes mod N
  var hash = 0;
  for (var i = 0; i < host.length; i++) {
    hash += host.charCodeAt(i);
  }
  return proxies[hash % proxies.length] + "; PROXY fallback.corp.com:8080";
}

Geo-routing by client subnet

Route clients on different office subnets through their nearest regional proxy to minimise latency.

proxy.pac
function FindProxyForURL(url, host) {
  if (isPlainHostName(host)) { return "DIRECT"; }

  var myip = myIpAddress();

  if (isInNet(myip, "10.10.0.0", "255.255.0.0")) {
    return "PROXY proxy-eu.corp.com:8080";   // London office
  }
  if (isInNet(myip, "10.20.0.0", "255.255.0.0")) {
    return "PROXY proxy-us.corp.com:8080";   // New York office
  }
  if (isInNet(myip, "10.30.0.0", "255.255.0.0")) {
    return "PROXY proxy-apac.corp.com:8080"; // Singapore office
  }
  return "PROXY proxy-default.corp.com:8080";
}

URL path-based routing

Route different URL patterns to different proxies. Note: path matching only works for HTTP — HTTPS strips the path before PAC evaluation.

proxy.pac
function FindProxyForURL(url, host) {

  // Route API traffic to a dedicated high-bandwidth proxy
  if ( shExpMatch(url, "http://api.example.com/*")
    || dnsDomainIs(host, ".api.example.com")) {
    return "PROXY api-proxy.corp.com:8080";
  }

  // Route streaming to a bypass proxy
  if ( dnsDomainIs(host, ".netflix.com")
    || dnsDomainIs(host, ".spotify.com")
    || dnsDomainIs(host, ".youtube.com")) {
    return "DIRECT"; // bypass proxy for high-bandwidth streaming
  }

  return "PROXY default.corp.com:8080";
}