GCP App Engine部署3个服务时dispatch.yaml配置访问404问题求助
GCP App Engine路由404问题解决方案
问题根因
你遇到的404错误由多个配置问题共同导致:
- dispatch.yaml的路由规则仅做精确匹配,没有覆盖路径后缀场景,且子服务收到的请求会携带路径前缀
- 子服务的静态资源 handler 未适配带前缀的请求路径,无法定位到build目录下的对应资源
- 默认服务的handler配置规则有误,无法正常匹配根路径外的静态资源
- 你访问的
/task路径没有匹配的dispatch规则,自然无法转发到对应服务
调整步骤
1. 修改dispatch.yaml配置
调整路由规则,覆盖带后缀的路径场景,修改后配置如下:
dispatch: - url: "abanoub-ayoub.com/" service: default # 匹配/ui-challenge开头的所有路径 - url: "*/ui-challenge/*" service: ui-challenge - url: "*/ui-challenge" service: ui-challenge # 匹配/test开头的所有路径 - url: "*/test/*" service: test-service - url: "*/test" service: test-service # 若需要/task路径指向test-service,新增以下规则 - url: "*/task/*" service: test-service - url: "*/task" service: test-service
2. 修改子服务配置
ui-challenge.yaml调整
转发到ui-challenge服务的请求会携带/ui-challenge前缀,需要在handler中剥离前缀才能匹配到build目录下的资源:
runtime: nodejs16 service: ui-challenge handlers: - url: /ui-challenge static_files: build/index.html upload: build/index.html - url: /ui-challenge/(.*)$ static_files: build/\1 upload: build/(.*)
test-service.yaml调整
同理适配前缀,如果你配置了/task指向该服务,也需要同步加对应的handler规则:
runtime: nodejs16 service: test-service handlers: - url: /test static_files: build/index.html upload: build/index.html - url: /test/(.*)$ static_files: build/\1 upload: build/(.*) # 若支持/task路径,新增以下规则 - url: /task static_files: build/index.html upload: build/index.html - url: /task/(.*)$ static_files: build/\1 upload: build/(.*)
3. 修改默认服务app.yaml配置
原配置的static_dir规则仅匹配根路径,调整后支持所有子路径资源匹配:
runtime: nodejs16 handlers: - url: / static_files: build/index.html upload: build/index.html - url: /(.*) static_files: build/\1 upload: build/(.*)
4. 重新部署配置生效
按顺序执行部署命令:
- 先部署调度规则:
gcloud app deploy dispatch.yaml - 重新部署三个服务:
gcloud app deploy app.yaml ui-challenge.yaml test-service.yaml - 等待2-5分钟规则生效后再测试访问即可。
内容的提问来源于stack exchange,提问作者Abanoub Ayoub
相关产品推荐
相关产品推荐

