Multiple custom domains to specific version of Google App Engine app - google-app-engine

Let's say I have myapp.appspot.com and two custom domains respectively called foo.com and bar.com. How do I configure Google App Engine (GAE) such that:
(www.)foo.com -> foo.myapp.appspot.com
(www.)bar.com -> bar.myapp.appspot.com
(www.)foo.com -> myapp.appspot.com (default version)
I'm reading https://developers.google.com/appengine/docs/domain but I still don't understand how to configure it. I get the impression that GAE only supports wildcard for one custom domain e.g. **.foo.com.

You can't really do that directly, as you associate your custom domain with an App ID rather than the App URL.
I guess you could map both foo.com and bar.com to your App ID, then in the default version of your App Engine parse the URL, and redirect accordingly, but it's not a great solution as you would be redirecting from your custom domain back to appspot.com domain.

You can route using the dispatch file (dispatch.yaml).
This blog post gave me the necessary info, and I imagine for the case of needing to map multiple domains to different modules would require a dispatch.yaml something like this:
# Dispatch
# ========
---
dispatch:
- url: 'foo.com/*'
module: foo
- url: 'bar.com/*'
module: bar
Don't forget to add the custom domains as well as SSL certs in the App Engine console.

Related

App Engine custom domain with service

I set up a custom domain with App Engine after following the instructions on DNS records:
https://cloud.google.com/appengine/docs/standard/java/mapping-custom-domains
It's working for the default service using URL
http://MY_CUSTOM_DOMAIN but I'm unable to access other services with URLs http://SERVICE_ID.MY_CUSTOM_DOMAIN. Do I need to map a different set of DNS records?
Here's a step-by-step:
Ensure you are a owner of the parent domain in here: https://www.google.com/webmasters/verification/home (e.g for subdomain.example.com you need to own example.com)
If not, ask a friend to add you as an owner.
Create a CNAME record pointing to: ghs.googlehosted.com (in Cloud DNS or wherever).
Note: This will cause downtime, but it is required for App Engine to create an SSL certificate so downtime is unavoidable...
Add custom domain to App Engine
Go to App Engine -> Settings -> Custom Domains
Click on Add a custom domain
Choose the verified parent domain and click Continue
Enter the subdomain (e.g subdomain.example.com) and click Save mappings
Click Done (you already did this last step)
Update App Engine dispatch rules
Run gcloud app describe --project <GOOGLE_CLOUD_PROJECT_ID> and take a look at the current section of dispatchRules
Create dispatch.yaml or dispatch.yml file with your new rule as well as the ones already existing above (note the file format is different from the command output)
dispatch:
- url: <DOMAIN>/* # e.g. subdomain.example.com/*
service: <SERVICE-NAME>
Deploy it using: gcloud app deploy dispatch.yaml --project <GOOGLE_CLOUD_PROJECT_ID>
Check how to use subdomains and how wildcard mapping works in the GCP docs here (actually these are the next steps at the website which you've followed and linked). Make sure that your DNS provider permits wildcards in CNAME host entries, otherwise such mapping will not be possible.
This blog post shows a real-life example of matching subdomains to services with App Engine.

How to add subdomains to Google App Engine with Flask and show different content at site and sub-domain?

We have a site developed with Flask using Google App Engine. So we have naked domain mysite.com (which redirects to www.mysite.com) and subdomain www.mysite.com
Now we want to add another subdomain user.mysite.com, so we added it to App Engine Custom domains and registrar without any issues.
BUT we are seeing the same copy of web-site at user.mysite.com as a www.mysite.com (all same pages, just subdomain user. instead of www.)!
How can we show another pages at user.mysite.com (like Hello world!), not the same pages as at www.mysite.com?
What you are asking about is built-into Flask, as Blueprints:
http://flask.pocoo.org/docs/0.12/blueprints/
This does a good job of explaining the Blueprint setup for subdomains:
http://exploreflask.com/en/latest/blueprints.html
For some simple cases, you could just test the request. Flask has that built-in, too:
#app.before_request
def check_subdomain():
'''
This runs before all requests
'''
if "user." in request.environ.get('HTTP_HOST'):
SOME_GLOBAL = 'user'
return None
But, sounds like you want to use Blueprints. It is a very elegant solution.
A possible solution is to deploy the dispatch.yaml file as explained in this doc. By this way, you can route requests to your subdomains to a specific service based on the hostname in the URL.
For your specific case, this could be the dispatch.yaml file:
dispatch:
# For www. subdomain.
- url: "www.mysite.com/"
service: [www-subdomain-frontend-service]
# For user. subdomain.
- url: "user.mysite.com/"
service: [user-subdomain-frontend-service]

GCP Point Custom Domain to Specific App Engine Service

I currently have an Google App Engine Flexible project with four services. And when I map my custom domain to my project using the documentation https://cloud.google.com/appengine/docs/standard/python/mapping-custom-domains, it automatically points to the default service which is not the frontend application. How do I map it to a different service.
The answer from #dan isn't up to date anymore:
The naming of the dispatch.yaml file changed from 'module' to 'service', like this:
dispatch:
- url: "sub1.yourdomain.com/*"
service: web-app
You deploy the stand-alone-file via this command (it hasn't to be in a project folder):
gcloud app deploy dispatch.yaml
Reference: https://cloud.google.com/appengine/docs/standard/python/config/dispatchref
You cannot map a certain (sub)domain to a certain service in the app-level custom domain mapping, mapping is done only at the app level (as a whole).
To direct a certain (sub)domain to a certain service inside your app you'll need to use a dispatch file, for example:
dispatch:
- url: "example.com/*"
module: <frontend-service-name>
Side note: you may want to revisit the decision of handling the frontend in a non-default service: the frontend is IMHO best suited to handle any garbage request coming in (which would typically not match any routing rule and would thus be directed towards the default service). If your default service does something more sensitive than the frontend it might not like that spam coming in.

Google App Engine module and custom domain

I'm trying to assign custom domain to App Engine module. At the moment I have staging.example.com pointed to app-id.appspot.com and that works correctly but I also want to assign api.staging.example.com to api.app-id.appspot.com. I've created CNAME record from api.staging.example.com to ghs.googlehosted.com, added api.staging.domain.com in developers console/appengine/settings/custom domains and here is my dispatch.yaml:
dispatch:
- url: "staging.example.com/*"
module: default
- url: "api.staging.example.com/*"
module: api
Any ideas what could be wrong? Every request to endpoints on api.staging.example.com shows only 404 error and I can't see this in the logs, it looks like api.staging.example.com is pointed to somewhere else, all request to api-app-id.appspot.com works correctly.
Are you making HTTPS requests? The official docs note that double-wildcard domains are not supported for SSL certificates.
Google recommends using the HTTPS protocol to send requests to your app. Google does not issue SSL certificates for double-wildcard domains hosted at appspot.com. Therefore with HTTPS you must use the string "-dot-" instead of "." to separate subdomains
So you'll need to replace the first . with -dot- to follow this pattern:
https://module-dot-app-id.appspot.com. In your case api-dot-app-id.appspot.com.
OK, I know where is my problem - Google Cloud Endpoints.
Google Cloud Endpoints does not support custom domains.
https://cloud.google.com/appengine/docs/python/endpoints/
https://code.google.com/p/googleappengine/issues/detail?id=9384

Custom domain mapping to Google App Engine module or version

Suppose I have a Google App Engine application which has several modules which have several versions. Can I map a custom domain name to a specific version of a specific module of the application?
For example:
http://www.example.com should be mapped to http://module1-dot-app1.appspot.com
1.Mapping a module to custom domain
Let's suppose you are admin for domain example.com and you want to map subdomain www.example.com to module webmodule in your application myapp.appspot.com
www.example.com -> webmodule.myapp.appspot.com
What you can do is going to admin.google.com and in App Engine Apps section configure your application so that it is mapped on web address www.example.com. Of course follow the instruction for configuring your domain (you should add a CNAME entry in your DNS configuration with ALIAS www and HOST ghs.googlehosted.com).
Once you did this, web address www.example.com is linked to your application default module. For redirecting to webmodule you need to configure application internal routing with dispatch file, as reported here:
https://developers.google.com/appengine/docs/python/modules/routing
In your case what you need is something like this (this is from dispatch.yaml file for Python application):
dispatch:
- url: "www.example.com/*"
module: webmodule
2.Mapping a version to custom domain
Regarding versions, it seems you cannot map an url to a specific module version. What you can probably do is mapping an url including version to a specific module, but this is maybe little bit confusing.
As thetonrifles already pointed out, you must use the dispatch.yaml file, which goes in your default module.
However, I also came across some issues when using secure domains/subdomains. If you've already uploaded your SSL cert for your custom domain, and you add custom subdomains after the fact, you need to go back to the SSL cert section and enable those subdomains you added, otherwise they'll just keep showing blank.

Resources