我发现它在烧瓶当前的稳定版本Flask==0.12.2
中的支持不是很好。理论上可以用host_matching来完成。但在我的测试中,静态路线总是被打破。
但是,编写本文时,master上的烧瓶开发版本合并了一个拉动请求,使得它更容易一些。做pip install git+git://github.com/pallets/flask.git
将安装Flask==0.13.dev0
。然后,使用工厂模式创建瓶子应用程序,您可以在我的情况下设置host_matching=True
和static_host=127.0.0.1:8000
。
对于我来说,我的工厂函数如下:
def create_app(config_obj=None):
"""An application factory, as explained here:
http://flask.pocoo.org/docs/patterns/appfactories/.
:param config_object: The configuration object to use.
"""
app = Flask(__name__, host_matching=True, static_host='127.0.0.1:8000')
app.config.from_object(config_obj)
register_extensions(app)
register_blueprints(app)
return app
,使这项工作所需要的另一件事是修改您的主机,并可以设定愿意在主机文件中引用的域。在窗口上,这可以在C:\Windows\System32\drivers\etc\hosts
找到。在hosts文件的底部,我已经修改为这样:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 alpha.local
127.0.0.1 beta.local
127.0.0.1 local
你需要运行背后的反向代理(NGINX在Windows上的Linux或IIS)这种解决方案,并将它设置转发相应的请求在本例的情况下为alpha.local:8000
或beta.local:8000
。但是,您将根据实际需要修改<subdomain>.local:<port>
。
您将面对的另一个问题是浏览器抱怨CORS请求,因此您可能需要为Access-Control-Allow-Origin: *
或特定域名如Access-Control-Allow-Origin: http://beta.local:8000
设置标头。对于开发服务器,我发现这是很有帮助的CORS允许字体访问:
@blueprint.after_app_request
def add_headers_to_fontawesome_static_files(response):
"""
Fix for font-awesome files: after Flask static send_file() does its
thing, but before the response is sent, add an
Access-Control-Allow-Origin: *
HTTP header to the response (otherwise browsers complain).
"""
if not os.environ.get('APP_ENV') == 'prod':
if request.path and re.search(r'\.(ttf|woff|woff2|svg|eot)$', request.path):
response.headers.add('Access-Control-Allow-Origin', '*')
return response
注意,对于生产,你必须设置你的代理服务器(如NGINX或IIS)和上面的函数修改后的头是不是有用加工生产时。
末,随着host_matching=True
那么路线必须为主机指定,例如低于:
@blueprint.route('/about/', methods=['GET', 'POST'],
host='<string:subdom>.local:8000')
def about_app(**kwargs):
"""The about page."""
return render_template('about.html')
如果你按照上面的路线,是有帮助的地方设置url_defualts的功能在你的应用,如下所示:
@blueprint.app_url_defaults
def add_subdom(endpoint, values):
path = request.url_root
if 'alpha.local' in path:
g.subdom = 'alpha'
elif 'beta.local' in path:
g.subdom = 'beta'
if current_app.url_map.is_endpoint_expecting(endpoint, 'subdom'):
values['subdom'] = g.subdom
祝你好运,这并不容易。