Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind one domain/subdomain per service in the Google App Engine?

I have a lot of trouble finding how to map multiple domains to multiple services in the GAE. Here is the configuration :

  • One application is a Go API, deployed in GAE in the standard environment
  • The second application is an Angular application, also deployed in GAE in the standard environment but as another service.

Here are the app.yaml files :

Go application app.yaml

runtime: go
api_version: go1.9

handlers:
- url: /.*
  script: _go_app

Angular application app.yaml

service: stage
runtime: python27
api_version: 1
threadsafe: true

skip_files:
- ^(?!dist)  # Skip any files not in the dist folder

handlers:
# Routing for bundles to serve directly
- url: /((?:inline|main|polyfills|styles|vendor)\.[a-z0-9]+\.bundle\.js)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*

# Routing for a prod styles.bundle.css to serve directly
- url: /(styles\.[a-z0-9]+\.bundle\.css)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*

# Routing for typedoc, assets and favicon.ico to serve directly
- url: /((?:assets|docs)/.*|favicon\.ico)
  secure: always
  redirect_http_response_code: 301
  static_files: dist/\1
  upload: dist/.*

# Any other requests are routed to index.html for angular to handle so we don't need hash URLs
- url: /.*
  secure: always
  redirect_http_response_code: 301
  static_files: dist/index.html
  upload: dist/index\.html
  http_headers:
      Strict-Transport-Security: max-age=31536000; includeSubDomains
      X-Frame-Options: DENY

I have a domain and want to bind the Go API to api.domain.com and the Angular app to domain.com.

By going to App Engine > Settings > Custom Domains I managed to add the domain for my API and it is perfectly working.
But now, I cannot find a way to map domain.com to my Angular application. Going to the same settings does not gives me an option to map a different service to my domain.

Thanks for the help and have a nice day !

like image 331
tomrcht Avatar asked May 16 '18 07:05

tomrcht


People also ask

How do I create a sub domain in Google Cloud Compute Engine?

In the Google Cloud console, go to the Custom Domains tab of the App Engine Settings page. Click Add a custom domain. If your domain is already verified, the domain appears in the Select the domain you want to use section. Select the domain from the drop-down menu and click Continue.

How do I add a DNS subdomain to Google cloud?

In the Google Cloud console, go to the Cloud DNS zones page. Click the name of the managed zone that you want to add the record to. On the Zone details page, click Add record set. On the Create record set page, in the DNS name field, enter the subdomain of the DNS zone—for example, mail .


2 Answers

To map subdomains you can use a dispatch.yaml file. An example:

dispatch:
    - url: "example.com/*"
      service: default

    - url: "api.example.com/*"
      service: otherservice

And then run $ gcloud app deploy dispatch.yaml (it can be in any directory).

Once you have example.com added under App Engine > Settings > Custom Domains for the default service, you can add the subdomain api.example.com for the other service. Later you need to add the new subdomain DNS records to you domain registrar as pointed out in the console configuration.

like image 55
Federico Panunzio Avatar answered Sep 28 '22 09:09

Federico Panunzio


You want to first map your naked domain.com to your app.

Then choose to add another domain and you'll have the option to add the api (or any other) domain.com subdomain to a specific service.

Note that you have a conflicting/overlapping handler pattern in the 2 services: - url: /.*, this won't work as GAE won't know to which service to direct such requests to, they'll all end up sent to the same service. You need to partition your requests URL namespaces in a non-overlapping manner and you'll likely need a dispatch.yaml file as well. See Mapping subdomain to a service in Google App Engine project for details.

like image 22
Dan Cornilescu Avatar answered Sep 28 '22 08:09

Dan Cornilescu