11

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 !

tom
  • 745
  • 5
  • 20

2 Answers2

31

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.

Inzamam Malik
  • 3,238
  • 3
  • 29
  • 61
  • Hey, thanks a lot for the answer ! I cannot find how to add the subdomain for the other service though, even if I use the `dispatch.yaml` file, in the Custom Domains tab I can only manage my default service, is there something I forgot to do ? – tom May 16 '18 at 19:23
  • 1
    If you already have example.com verified and added, all you need to do is go to App Engine > Settings > Custom Domains and select “Add a custom domain” again. It will ask you if you want to use the already verified example.com or if you want to verify a new domain. Select example.com and it will ask you the subdomain of example.com you want to add, in this case api.example.com. – Federico Panunzio May 17 '18 at 07:35
  • I see ! With the `dispatch.yaml` file this will redirect the traffic to the corresponding service, thanks a lot ! – tom May 17 '18 at 08:27
  • Thanks @Federico. I encountered similar situation & your answer helped in time.! – Vinayak Singh Feb 25 '19 at 17:54
1

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.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • I see, the `dispatch.yaml` file will be used to redirect traffic to a service or another, thanks, I'll take a look into it ! – tom May 16 '18 at 14:46
  • I think handler pattern doesn't matter since they are two independent service – Brady Huang Feb 08 '23 at 17:11
  • I mean shouldn't handler pattern will be mapped according to two independent service url? Like default `-url: /.*` will map to `domain.com` while api `-url: /.*` will map to `api.domain.com` – Brady Huang Feb 09 '23 at 03:33
  • @BradyHuang there is a single `dispatch.yaml` file in a project, not one per service... – Dan Cornilescu Feb 16 '23 at 13:16
  • @DanCornilescu The context is your statement `Note that you have a conflicting/overlapping handler pattern in the 2 services: - url: /.*`, but the question is, each service own its handler isn't it? Why would they conflict? – Brady Huang Feb 16 '23 at 14:56