How to setup a reverse proxy via Cloudflare?

Note: always replace “client.com” with your hostname, “client.backend.verbolia.com” with the given verbolia hostname and “/path_to_verbolia/” with the agreed routed url pattern.

  • On your dash.cloudflare.com UI, go to Workers Routes and then click Manage Workers

         a39f3215-f174-4c4b-a115-a0364b1cd863
02950c26-b6c6-44ff-b082-c7c75c0a6571

  • Click Create a service, choose a service name, HTTP handler and click Create service

        88968f5d-5b08-4645-a636-dfeb2f5d0264
992043f7-92f2-433b-9c7f-480ae3d5cbf6

  • Click Quick edit, copy paste the following code in the editor (left side - don’t forget to adapt the const ORIGINS parameters) and click Save and deploy

        ebb0196a-3bd5-4b3f-b302-d8835becbb21

 

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

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

addEventListener('fetch', event => { 
event.respondWith(handleRequest(event.request)) 
}) 
/** 
* An object with different url's to fetch 
* @param {Object} ORIGINS 
*/ 
const ORIGINS = { 
'www.client.com': 'client.backend.verbolia.com', 
}

 

  • Go back to Worker Routes page and click Add route

     

    91864949-0652-4f55-803f-e1c267a4b433

  • Put your hostname followed by the agreed route pattern in Route, choose “reverse-proxy-verbolia” in Service and click Save.