使用Google App Engine将所有请求从一个域重定向到另一个域,但将静态路由规则保留在yaml中 [英] redirect all requests from one domain to another with Google App Engine but keep static routing rules in yaml

查看:64
本文介绍了使用Google App Engine将所有请求从一个域重定向到另一个域,但将静态路由规则保留在yaml中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GAE应用,该服务在yaml文件中的规则下定义静态文件,该域名在DNS中配置的两个不同域名下,一个旧域名,一个新域名,但是为每个域名提供的内容都是相同的.我想将请求从旧域重定向到新域.我见过这个问题,但是失败了据我所知,能够在Yaml中使用静态资产处理程序,并且必须在我认为的main.py中显式设置静态资产服务.当主机名是旧域时,是否有一种简单的方法(最好是在yaml文件本身中)进行重定向,但是对于新域,请保留我的静态文件规则?

更新

这是我最终使用的完整解决方案:

  ### dispatch.yaml ###派遣:-网址:"* my.domain/*"模块:重定向模块### redirector.yaml ###模块:重定向模块运行时:python27线程安全:trueapi_version:1skip_files:-^(?! redirector.py $)处理程序:#通过重定向器重定向所有内容-网址:/.*脚本:redirector.app### redirector.py ###导入webapp2def get_redirect_uri(handler,* args,** kwargs):返回'https://my.domain/'+ kwargs.get('path')app = webapp2.WSGIApplication([webapp2.Route('/&path;.*>',webapp2.RedirectHandler,默认= {'_ uri':get_redirect_uri}),],debug = False) 

一些额外的文档: https://cloud.google.com/appengine/docs/python/modules/routing#routing_with_a_dispatch_file

解决方案

对于AFAIK,您不能为静态资产进行重定向,因为GAE会直接根据.yaml文件规则为其提供服务,甚至不会触及您的应用程序代码./p>

您可以在您的应用中添加一个模块(例如,将其称为 redirect-module ),使用调度程序文件将所有旧域URL路由至该应用,并在此模块中使用动态处理程序来重定向指向新域等效项的URL,以及您所引用问题的答案中建议的内容.新的域请求将保持不变,无论是作为静态资产还是您应用程序的现有模块,都可以继续工作. dispatch.yaml 文件如下所示:

 应用程序:您的应用程序名称派遣:-网址:"your.old.domain.com/*"模块:重定向模块 

另一个想到的想法(我实际上没有这样做,所以我不确定它是否可以解决您的问题)是完全避免重定向,而不是将您的应用程序映射到2个不同的域,而仅将其映射到新域,并使旧域成为新域的DNS CNAME/别名.

I have a GAE app serving static files defined by rules in the yaml file under two different domain names as configured in DNS, an old one and a new one, but otherwise it's the same content served for each. I'd like to redirect requests from the old domain to the new domain. I've seen this question, but that loses the ability to use the static asset handlers in the yaml from what I can tell, and would have to set up static asset serving explicitly in my main.py I think. Is there a simple way (ideally in the yaml file itself) to do a redirect when the hostname is the old domain, but keep my static file rules in place for the new domain?

Update

Here's a complete solution that I ended up using:

### dispatch.yaml ###

dispatch:
- url: "*my.domain/*"
  module: redirect-module

### redirector.yaml ###

module: redirect-module
runtime: python27
threadsafe: true
api_version: 1

skip_files:
- ^(?!redirector.py$)

handlers:
# Redirect everything via our redirector
- url: /.*
  script: redirector.app

### redirector.py ###

import webapp2

def get_redirect_uri(handler, *args, **kwargs):
    return 'https://my.domain/' + kwargs.get('path')

app = webapp2.WSGIApplication([
    webapp2.Route('/<path:.*>', webapp2.RedirectHandler, defaults={'_uri': get_redirect_uri}),
], debug=False)

Some extra docs: https://cloud.google.com/appengine/docs/python/modules/routing#routing_with_a_dispatch_file

解决方案

AFAIK you can't do redirection for the static assets, since GAE serves them directly according to the .yaml file rules, without even hitting your app code.

You could add a module (let's call it redirect-module for example) to your app, route ALL old domain URLs to it using a dispatcher file and use a dynamic handler in this module to redirect URLs to the new domain equivalents, along the lines suggested in the answers to the question you referenced. The new domain requests will continue to work unmodified, served either as static assets or the existing module(s) of your app. The dispatch.yaml file would look like this:

application: your-app-name
dispatch:
  - url: "your.old.domain.com/*"
    module: redirect-module

Another thought that comes to mind (I didn't actually do this, so I'm unsure if it would address your problem) is to avoid the redirect altogether and instead of mapping your app to 2 different domains map it only to the new domain and make the old domain a DNS CNAME/alias to the new domain.

这篇关于使用Google App Engine将所有请求从一个域重定向到另一个域,但将静态路由规则保留在yaml中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆