Frenchcooc
Frenchcooc

Reputation: 1080

Custom domains / catch all hostnames on Google App Engine

I'm trying to configure my Google App Engine instance with Cloudflare for Saas, and more precisely Cloudflare's SSL for SaaS offering. The objective being that I can provide to my customer a "custom domain" (also known as "vanity domain"), such that they don't go to dashboard.mywebsite.com, but instead app.customerwebsite.com.

Configuration part

To make sure that my App Engine instance is correctly serving content on dashboard.mywebsite.com, I've made the following:

On Google Cloud side:

Google Cloud custom domain configuration

runtime: nodejs14

env_variables:
  NODE_ENV: 'production'

basic_scaling:
  max_instances: 10
  idle_timeout: 5m

On Cloudflare side:

Cloudflare DNS zone

I waited for a few hours and I confirm that dashboard.mywebsite.com resolves correctly and serves my content (from Google App Engine).

Next, custom domains

According to Cloudflare documentation, I had to register the fallback origin (i.e. dashboard.website.com) and then configure a custom hostname (e.g. app.customerwebsite.com). Which I did.

Custom Hostnames configuration in Cloudflare

Now, according to Cloudflare documentation again, my customer has to create a CNAME record. Which I did with a domain of mine:

app.customerwebsite.com CNAME dashboard.mycompany.com

The issue

I waited a few hours again. Then, when I open app.customerwebsite.com in my browser, it shows a Google 404 error page instead of my dashboard. Which makes me think that Cloudflare successfully "redirects" the traffic to Google, but App Engine refuses to serve it. Probably because it doesn't know app.customerwebsite.com?

Any thoughts that would help?

Upvotes: 2

Views: 955

Answers (1)

Frenchcooc
Frenchcooc

Reputation: 1080

As you noticed, the issue is not related to Cloudflare, but App Engine. The problem with your configuration is that, when App Engine receives a request, based on the Host header, it forwards the request to the right instance.

App Engine lets you map any custom domains that has been previously validated by Google. But in your situation, that would mean you have to register each custom domain of your customers on your App Engine instance. That's too cumbersome (if even possible).

What you need to do instead is the following:

  1. enable a static IP address with Google Cloud
  2. change your DNS record from dashboard CNAME ghs.googlehosted.com to dashboard A YOUR_IP_ADDRESS
  3. configure a Google Cloud Load Balancer to map requests received on that IP address to your App Engine instance.

Google's documentation has a great guide on how to setup a load balancer with Cloud Run. By changing a few settings it works great with App Engine. As an extra help, below is the configuration details of our load balancer that allows us to provide vanity domains / custom domains to our customers through Google Cloud:

Google Cloud Load Balancer configuration details

Again, the load balancer is here responsible to map all requests received by your IP address (no matter the Host header) straight to your App Engine instance.

As a best practice, it might be useful to push a dispatch.yaml file to your instance:

dispatch:
  - url: '*/*'
    service: default

Which tells App Engine to send all requests to the default service. It works a bit like a wildcard virtual hosts on an Apache server.

Upvotes: 5

Related Questions