Routing to other hostnames

Hi all,

I’m investigating the ways to move our existing reverse proxy setting onto Cloudflare. Currently, we have to 2 servers behind the proxy, the App (Heroku based) and the Shop (BigCommerce). The reverse proxy with simple nginx setup dispatches (via proxy_pass) requests between the App and the Shop based on request URI. Each app has own hostname, e.g. myapp.herokuapp.com for the App, and storeXXX-mybigcommerce.com for the Shop.

And nginx config looks like

location {
proxy_pass https://myapp.herokuapp.com
}

location {
proxy_pass https://storeXXX-mybigcommerce.com
}

Is it possible to implement such setup using Workers? I specifically would like to know if there is a limitation of fetching requests to hosts not matched with the account domain?

Appreciate any help!

Thanks,
Nick

I’ve yet to see Workers used as a general proxy, you’d have to redirect all of the URI’s to match that of the target server and if you’re thinking of combining two different domains into one - that’s very difficult if resources share the same path.

The limit here is that Workers can’t change its host header except if the domain is added to the same Cloudflare account and active (or is the same domain, I believe). @KentonVarda should be able to confirm this.

In a worker you can do fetch a specific url

Building a proxy, like the static website example.

async function handleRequest(request) {
  const parsedUrl = new URL(request.url)
  let path = parsedUrl.pathname

  let lastSegment = path.substring(path.lastIndexOf('/'))
  if (lastSegment.indexOf('.') === -1) {
    path += '/index.html'
  }

  return fetch("https://Cloudflare-example-space.nyc3.digitaloceanspaces.com" + path)
}

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

Yes, but it will send the Host header equal to your website, works on public buckets, but not on third party sites.

2 Likes

I’m not going to change its host header, both apps should know real host. So, it seems all should be good.

Thanks!