将子域映射到Google App Engine项目中的服务 [英] Mapping subdomain to a service in Google App Engine project
问题描述
我有一个带有以下yaml文件的Google App Engine项目
I have a Google App Engine project with following yaml file
handlers:
- url: /web/.*
script: web_server.app
- url: /api/.*
script: rest_server.app
如何确保rest_server.app脚本为我拥有的域的子域提供服务.
How do I make sure subdomain, of a domain I own, be served by rest_server.app script.
示例:如果我拥有example.com
Example: If I own example.com
我希望example.com
由web_server.app
服务,而api.example.com
由rest_server.app
是否可以使用Google App Engine做到这一点.
Is it possible to do that using Google App Engine.
示例:
handlers:
- url: example.com/.*
script: web_server.app
- url: api.example.com/.*
script: rest_server.app
推荐答案
app.yaml
中的请求路由不能用于基于URL的域名进行路由,请参见处理程序元素文档部分.
Request routing in the app.yaml
can not be used to route based on the URL's domain name, see the url
table row in the Handlers element doc section.
因此,您无法真正为应用程序提供单个模块/服务,同时剥离了处理程序的url
配置中当前用于将请求路由到一个脚本或脚本的URL的文件路径部分.其他.
Because of that you can't really have a single module/service serving your app while simultaneously stripping the filepath portion of the URL you're currently used in your handlers' url
configs for routing requests to one script or the other.
通过将应用程序分为2个单独的服务/模块,每个服务/模块都处理一个脚本.其中一个模块必须是默认模块,我将web
设置为默认模块.
dispatch.yaml
文件将用于将请求路由到各自的模块基于URL主机名.
You can obtain what you desire by splitting your app into 2 separate services/modules, each handling one script. One of the modules has to be the default module, I'd make the web
one the default.
A dispatch.yaml
file would be used to route requests to their respective modules based on the URL hostname.
web.yaml
文件将包含:
module: default
handlers:
- url: /.*
script: web_server.app
rest.yaml
文件将包含:
module: rest
handlers:
- url: /.*
script: rest_server.app
在dispatch.yaml
文件中,您仅需要非默认模块的路由,默认情况下,将不匹配任何路由的请求路由到defaut模块:
In the dispatch.yaml
file you only need routes for the non-default module(s), requests matching no routes are by default routed to the defaut module:
- url: "api.example.com/*"
module: rest
您可以在此处找到更完整的示例: https://stackoverflow.com/a/34111170/4495081
You can find a more complete example here: https://stackoverflow.com/a/34111170/4495081
然后,您将example.com
裸域和api.example.com
子域都映射到您的应用.请按照为您的应用程序添加自定义域程序,请特别注意配置裸域与子域时稍有不同的部分.另请参阅 https://stackoverflow.com/a/36317462/4495081
You'd then map both your example.com
naked domain and api.example.com
subdomain to your app. Follow the Adding a custom domain for your application procedure, paying extra attention to the sections which are slightly different when configuring a naked domain vs a subdomain. See also https://stackoverflow.com/a/36317462/4495081
存在一个问题,因此-基于主机名的dispatch.yaml
路由不适用于本地开发服务器,发往rest
模块的请求实际上将转到
There is one problem, tho - dispatch.yaml
routing based on hostnames doesn't work with the local development server, the requests destined for the rest
module would actually go to the default
module.
一个更简单的解决方法是将rest
模块客户端定向到本地devserver的rest
模块侦听的实际localhost:PORT URL(在dev服务器启动时显示在终端中).
A simpler workaround would be to direct the rest
module clients to the actual localhost:PORT URL where the local devserver's rest
module listens (displayed in the terminal at dev server startup), instead.
并非在所有情况下或对于所有应用程序都可能 .例如,如果应用使用自动生成的URL进行跨模块请求,就会出现问题.
This might not be possible in all cases or for all apps. For example it's an issue if the app makes cross-module requests with auto-generated URLs.
在这种情况下,要解决此问题,您可以在rest.yaml
URL中临时插入一个小路径部分,只有在本地dev服务器上测试rest
模块时(您需要在客户端进行匹配的更改)和/或跨模块URL生成逻辑):
In such cases, to work around it you can temporarily insert a small path portion in the rest.yaml
URL, only during testing on the local dev server the rest
module (you'd need matching changes on the client side and/or the cross-module URL generation logic):
module: rest
handlers:
- url: /api/.*
script: rest_server.app
然后您可以添加一个不基于主机的dispatch.yaml
规则,该规则也可以与本地开发服务器一起使用.可以将其永久保留在此处,如果/当临时rest.yaml
更改被撤消在生产环境中部署时,不会受到损害:
And then you can add a dispatch.yaml
rule that is not host-based and would also with the local dev server. This can be left in there permanently, it doesn't hurt if/when deployed in production when the temporary rest.yaml
change is reversed:
- url: "api.example.com/*"
module: rest
- url: "*/api/*"
module: rest
这篇关于将子域映射到Google App Engine项目中的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!