How to bind one domain/subdomain per service in th

2020-07-09 06:25发布

问题:

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 !

回答1:

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.



回答2:

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.