Setting up reverse proxy for Datahub.io/blog

Setting up reverse proxy for Datahub.io/blog

We want datahub.io/blog running off pages.datahub.io/blog

In order to achieve this, we used Cloudfare workers to set up a reverse proxy. This means anytime a user visits datahub.io/blog, we server them pages.datahub.io/blog. The following steps can be taken to setup cloudfare worker for reverse proxy:

  • Go to the Cloudfare dashboard
  • Click on workers on the side menu

  • Click on create service

  • Give the service a name, and then click create service

  • On the service page, click on Quick edit

  • In the code editor, insert and test your proxy code. The following code is used to proxy datahub.io/blog to pages.datahub.io/blog:
addEventListener('fetch', function(event) {

event.respondWith(handleRequest(event.request))

})

  

async function handleRequest(request) {

  const { pathname } = new URL(request.url)
  
  console.log(pathname)
  
  // Only GET requests work with this proxy.
  
  if (request.method !== 'GET') return MethodNotAllowed(request)
  if (pathname == "/blog") return fetch(`https://pages.datahub.io/blog`) 
  
  return fetch(`https://datahub.io${pathname}`)

}

function MethodNotAllowed(request) {
    return new Response(`Method ${request.method} not allowed.`, {
          status: 405,
          headers: {
              'Allow': 'GET'
          }
    })
}

  • Test the code to ensure it works, then click on Save and Deploy, and go back to the service page
  • Click Triggers, and the click Add route

  • Add the route you want to trigger, for example datahub.io/blog and set the zone to the corresponding domain, e.g datahub.io

  • Finally click Add Route, and test the proxy

© 2024 All rights reserved

Built with DataHub LogoDataHub Cloud