Search code examples
nginxproxyurl-rewritingwebservercloudflare

Can I use the same domain to proxy two different hosts?


I have a main domain, eg.: mydomain.com.
A Single Page Application: myApp, hosted on: myApp.somehost.com (IP: {ip.of.myapp}).
A WordPress Site: myWP, hosted on: myWP.anotherhost.com (IP: {ip.of.mywp}).

The goal:

  1. mydomain.com should show myWP (this is straight forward, I need an A record in my DNS NameServer, with {ip.of.mywp})
  2. mydomain.com/app should show myApp.

How would you solve the second one? Is there a frontend-proxy service provider or so?

What I tried:

  • Cloudflare CDN: rewriting the hostname is not allowed.
  • Vercel: here I can host static pages (myApp) and proxy certain subpaths. But not the root url! So mydomain.com can't show myWp

What should I do? Please help... Thanks

Note: I'm not planning to move myApp and myWP from their current servers.


Solution

  • You are correct about rewrite rules not supporting this with Cloudflare, but you can use Cloudflare Workers to act as a proxy. Here is a blog article going over details of one way you could achieve what you are looking for -

    https://blog.cloudflare.com/subdomains-vs-subdirectories-improved-seo-part-2/

    Here are also a number of other Worker examples that may come in handy -

    https://developers.cloudflare.com/workers/examples

    Do keep in mind the pricing / limits of free plan if you go this route - https://developers.cloudflare.com/workers/platform/pricing