【发布时间】:2016-12-07 12:27:24
【问题描述】:
我有以下设置:
-
主服务器:
nginx转发"https://example.com/machine1/*" -> "http://machine1/*" -
机器 1:
ngnix + uwsgi + 烧瓶安装在/,例如/foobar。
这意味着https://example.com/machine1/foobar 将以/foobar 的身份访问Machine1。
我在变量NGINX_MOUNT_URL 中有安装点/machine1。
问题是,url_for 不能解决这个问题。
我试过了
app.config["APPLICATION_ROOT"] = NGINX_MOUNT_URL # = "/machine1"
但似乎只适用于 cookie 网址。
有一个简单的看法:
@app.route("/foobar")
def web_foobar():
from flask import url_for
return "The URL for this page is {}".format(url_for("web_foobar"))
# end def
因此,浏览https://example.com/machine1/foobar(主服务器)将被代理到显示The URL for this page is /foobar的http://machine1/foobar(Machine1)。
我需要它是 /machine1/foobar,以便在模板中链接才能工作。
我尝试了https://stackoverflow.com/a/18967744/3423324 提供的解决方案,但结果总是我必须浏览https://example.com/machine1/machine1/foobar 才能获得页面/foobar。
如何让flask.url_for 知道APPLICATION_ROOT?
【问题讨论】:
-
这是一个老问题,但如果其他人出现,我实现此目的的一种方法是在注册服务模块时传递
url_prefix='/machine1'(register_module后来改名为register_blueprint似乎)。