【问题标题】:Flask Proxy resolve url_forFlask 代理解析 url_for
【发布时间】: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 /foobarhttp://machine1/foobarMachine1)。 我需要它是 /machine1/foobar,以便在模板中链接才能工作。

我尝试了https://stackoverflow.com/a/18967744/3423324 提供的解决方案,但结果总是我必须浏览https://example.com/machine1/machine1/foobar 才能获得页面/foobar

如何让flask.url_for 知道APPLICATION_ROOT

【问题讨论】:

标签: python nginx flask


【解决方案1】:

看看How do I get the different parts of a Flask request's url?

pathscript_root 请求变量看起来适合您的需要。

【讨论】:

  • 问题是模板引擎在使用url_for生成它时,在url前面缺少/machine1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-04
  • 2018-01-06
  • 2014-08-08
  • 2016-10-18
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多