Is it possible to route to specific service in Google App Engine Flexible without creating a dispatch.yaml?
The documentation (https://cloud.google.com/appengine/docs/flexible/java/how-requests-are-routed#default_routing) says we can route to a specific service by calling http://SERVICE_ID.MY_CUSTOM_DOMAIN.
When I tried http://SERVICE_ID.MY_CUSTOM_DOMAIN it did not work. The request got routed to default and not the service.
So I tried dispatch.yaml and it worked.
dispatch:
- url: "SERVICE_ID.MY_CUSTOM_DOMAIN/*"
service: SERVICE_ID
Sending request to a service is default routing and it should have routed to the service (but did not work?). Why do we need dispatch.yaml file in this case?
If you specify a subdomain, it'll always point to the default service of your GAE.
To be able to route to each service using subdomains, you must use a wildcard mapping.
Related
How do I direct the www. subdomain to just domain.tld without www? I'm used to firebase doing this automatically. Should I look into configuring the app.yaml, dispatch.yaml, or another method?
What you're describing is called a "naked domain", and this is described in the documentation on Custom Domains. The documentation provides the steps for mapping a custom domain to your app and updating the DNS records at your domain registrar once your service has already been mapped to your custom domain in App Engine.
To redirect your requests, you can use wildcard mappings with services in App Engine by using the dispatch.yaml file. You can find instructions on how to do that here. If you would like to know more about routing requests, you can take a look at this documentation as well which also highlights creating a dispatch file. Handlers are limited to handle URLs by executing application code, or by serving static files uploaded with the code, such as images, CSS, or JavaScript. Therefore, they cannot directly redirect one URL to another.
You would need to handle your URL by running a script that executes code that will redirect your URL.
The comment shows a complete example as the script runs main.py which then redirects the URL
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]
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.
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
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.