Proxy Rudderstack API with Cloudflare

82 Views Asked by At

Currenty we have a Cloudflare account which serves our website and some other web apps. We use Cloudflare DNS to proxy the requests to the different pages/workers.

Now, we have a new request from our management, where we need to integrate a custom domain for the Rudderstack. In their documentation they write: You will need access to your domain’s DNS settings as well as your CDN settings. In Cloudflare I can change DNS settings to proxy from api.ourdomain.com to api.rudderlabs.com. But this does not work, I have a Cloudflare error saying: Invalid SSL certificate (526). All certificates are valid and working good. (I tried both Full and Full(Strict) options)

ssl cert error

Rudderstack suggest to use Amazon CloudFront to create a CloudFront distribution. Maybe I need to redo the whole DNS setup for our company and start from AWS CloudFront since it looks much more flexible?

Is it possible to do with Cloudflare or am I missing something?

dns config rudderstack

1

There are 1 best solutions below

0
srikanth On

You can setup a Cloudflare worker to do Bulk origin override and send the requests to the RudderStack data plane. Example code that would go in the worker:

const ORIGINS = {
    'abc.mydomain.com': 'xyz.dataplane.rudderstack.com',
};

function handleRequest(request) {
    const url = new URL(request.url);
    // Check if incoming hostname is a key in the ORIGINS object
    if (url.hostname in ORIGINS) {
    const target = ORIGINS[url.hostname];
    url.hostname = target;
    // If it is, proxy request to that third party origin
    return fetch(url.toString(), request);
    }

    // Otherwise, process request as normal
    return fetch(request);
}

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

In the cloudflare worker's Custom Domains setup your subdomain which will generate a certificate for you and should be ready to use