Cloudflare

Setting up Traffic Splitting with Cloudflare

Setup

To use Traffic Splitting with Cloudflare you will need to deploy a new Cloudflare Worker. This Worker will manage and direct traffic flow between Glopal Proxy and your service Origin.

Before considering using Cloudflare Workers to configure traffic splitting, ensure that Cloudflare CDN is enabled. You can verify this by checking that the "orange cloud" icon is active for your target site record under DNS settings.

Create a Worker

  1. Log in to the Cloudflare dashboard and select your account.

  2. Select Workers & Pages > Create application.

  3. Select Create Worker > Deploy.

Navigate to the newly created worker and click the "Edit Code" button. This will open the editor where you can write and deploy your Worker script. In the editor, you'll find a pre-filled script; delete it.

  1. Copy the following script and Paste it into the Workers editor.

  2. Make sure to replace GLOPAL_HOSTNAME placeholder with the value you receive from Glopal (for example store-1234.app.glopalstore.com) and that PATTERN is a regular expression describing list of in-scope paths you want to delegate to Glopal.

app.js
const GLOPAL_BACKEND_HOSTNAME = '';
const GLOPAL_PATHNAMES_REGEXP = new RegExp('^\/()($|[:/?])');

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const url = new URL(request.url);
  if (!url.pathname.match(GLOPAL_PATHNAMES_REGEXP)) {
    return fetch(request);
  }
  const newReq = new Request(request);
  newReq.headers.set('X-Forwarded-Host', url.hostname);
  url.hostname = GLOPAL_BACKEND_HOSTNAME;
  url.protocol = 'https:';
  return fetch(url.href, newReq);
}

Testing your rule

To ensure the rule works as expected, conduct a series of tests by visiting URLs that should be affected by the rule and confirming that they are rewritten correctly.

Setup Route

Under worker settings, navigate to Triggers > Routes > Add route.

Enter one or more routes that are matching domain and paths that should be delegated to Glopal. Make sure route ends with a * character.

As Worker code is already checking for "in-scope paths", you can setup a simple route like www.store.com/* to run all requests through the Worker. Alternatively you can configure multiple routes for each delegated path, for example www.store.com/de* and www.store.com/fr*.

Last updated