flask中使用子域名绑定blueprints

使用flask的blueprint 时,这里有个参数subdomain, 可以用来绑定子域名。

:param subdomain: A subdomain that blueprint routes will match on by
        default.

实践步骤如:
/etc/hosts配置域名,或真实场景 dns解析到主机上。

127.0.0.1 test.com
127.0.0.1 api.test.com

flask 的 app 实例 配置 SERVER_NAME, 这步是必须的,配置成根域名 test.com

app.config['SERVER_NAME'] = 'test.com:5000'

在blueprint声明中添加subdomain

api_gateway = Blueprint('api_gateway', __name__, subdomain='api')

到此,可以访问形如 api.test.com:5000, api.test.com:5000/news 这样的接口了。

实际场景中上述的5000端口可以配置为80或443, 访问时无须添加端口号。