Using Cloudflare as a reverse proxy

Created by Donald Ng, Modified on Tue, 20 Aug at 11:12 PM by Donald Ng

To use Cloudflare for reverse proxying with Mida.so, make sure that you're logged into your Cloudflare account, and that you've added your domain (called "website" in Cloudflare) to the account.


There are two ways to do this:

  1. Using Cloudflare Workers. This is a bit more setup, but can be used on all Cloudflare plans.
  2. Using DNS and Page Rules. This is the simplest method, but requires the Cloudflare Enterprise plan.


Method one (CF Free):


Proxy using Cloudflare Workers Workers are really powerful and allow up to 100,000 requests per day on the free plan (see Cloudflare pricing). Follow these steps to set up a reverse proxy worker:

  1. Create a worker From the root of the Cloudflare dashboard, go to "Workers & Pages" > "Overview" > "Create application" > "Create Worker". At this point, you can either keep the random worker name or choose your own. Click "Deploy" once done.
  2. Configure the worker to act as a proxy Click "Edit code" once the new worker has been saved following "Deploy". (And if you're already on the worker page, click "Quick edit".) You should now be seeing a code editor for the worker. Just replace all the existing content with this proxying code:
    const API_HOST = "cdn.mida.so"

    async function handleRequest(request, ctx) {
    const url = new URL(request.url)
    const pathname = url.pathname
    const search = url.search
    const pathWithParams = pathname + search

    return forwardRequest(request, pathWithParams)
    }

    async function forwardRequest(request, pathWithSearch) {
    const originRequest = new Request(request)
    originRequest.headers.delete("cookie")
    return await fetch(`https://${API_HOST}${pathWithSearch}`, originRequest)
    }

    export default {
    async fetch(request, env, ctx) {
    return handleRequest(request, ctx);
    }
    };

When done, click "Save and deploy".

  1. Use a custom domain for the worker This step is optional, but highly recommended. To use your own domain instead of the basic *.workers.dev one, go to the worker page (by exiting the code editor, if you're still in it) and there click "settings" -> "triggers". Click "Add Custom Domain", type in a subdomain, and save with "Add Custom Domain". The subdomain can be anything, even pineapple.yourdomain.com – just remember to avoid terms like "tracking" or "analytics", as they may be blanket-blocked.
  2. Use the new host in your Mida.so implementation You can now use your worker's domain (shown under "Preview" on the worker page) as md_cdn in your Mida.so implementation! If you've added a custom domain in step 3, use that instead.


Method two (CF Enterprise): 


Proxy using DNS and Page Rules with Cloudflare Enterprise Proxying traffic using DNS is relatively straight-forward. However, it requires correcting the Host headers for proxied requests, which is only available on the Cloudflare Enterprise plan. If your domain is on this plan, follow these steps:

  1. Add a DNS record Select your website in the Cloudflare dashboard, go to the "DNS" page, and there click "Add record". Make sure this is a CNAME record, and choose a name for it, which will be the Mida.so proxy subdomain. The subdomain can be anything, even watermelon.yourdomain.com – just remember to avoid terms like "tracking" or "analytics", as they may be blanket-blocked. The record should point to https://howuku.azureedge.net, and have proxy enabled.
  2. Correct Host headers The proxy won't work if the Host headers of requests aren't rewritten from your domain to the Mida.so domain. To fix this, add a Page Rule to change the Host header to cdn.mida.so.
  3. Use the new host in your Mida.so implementation You can now use your CNAME record's domain as md_cdn in your Mida.so implementation!


Finally, remember to update your Mida.so snippet as follows:

<script>
window.md_cdn = 'https://your-chosen-subdomain.yourdomain.com'
</script>
<script src="https://your-chosen-subdomain.yourdomain.com/js/optimize.js?key=XXXXXX" />

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article