【问题标题】:Flask redirect() sends invalid hostFlask redirect() 发送无效主机
【发布时间】:2017-04-19 21:30:03
【问题描述】:

我正在取出一个旧的 Flask 应用程序并试图让它运行。但是,flask.redirect() 让我有些头疼。这以前可以工作,也许最近的 Flask 版本有所改变?

当我访问应用程序索引页面 (/) 时,由于索引视图上的 requires_auth() 装饰器,它应该将我重定向到 /login(见下文)。它实际上确实重定向了我,但主机不正确。我最终不是flux.example.com/login,而是flux/login

def requires_auth(func):
  ''' Decorator for view functions that require basic authentication. '''

  from .models import Session, User

  @functools.wraps(func)
  def wrapper(*args, **kwargs):
    app.logger.warn('Checking authentication ..')  # added for debug purposes just now
    app.logger.warn('Note: url_for(\'login\'): {}'.format(url_for('login')))
    user_name = session.get('user_name')
    user_passhash = session.get('user_passhash')
    with Session() as db_session:
      user = User.get_by(db_session, user_name, user_passhash)
      if not user:
        return redirect(url_for('login'))

    request.user = user
    return func(*args, **kwargs)

  return wrapper

这是响应头:

Connection:keep-alive
Content-Length:219
Content-Type:text/html; charset=utf-8
Date:Wed, 19 Apr 2017 21:18:51 GMT
Location:http://flux/login
Server:nginx/1.6.2

在配置中设置SERVER_NAME=https://flux.example.com 不起作用。实际上,整个应用程序不再工作了,我访问的任何页面都得到了 404。

如何解决此重定向问题?

Python 3.6,Flask 0.10.1

【问题讨论】:

  • 如果您之前没有 server_name,我想知道它从哪里获得 http://flux。如果你搜索代码库是在任何地方指定的词通量?
  • @davidejones 我认为它可以抢夺该字符串的唯一地方是app = Flask(__name__),因为__name__ == 'flask'

标签: python redirect flask


【解决方案1】:

我认为你在正确的轨道上,但你应该只使用没有协议的主机名:SERVER_NAME=flux.example.com

【讨论】:

  • 不幸的是,我得到了与包含的方案相同的行为。这到底有什么变化?我的应用程序隐藏在 NGinx 代理后面,为 prox_pass http://localhost:5001$request_uri;,也许这会导致一些问题?我尝试添加 these 以防 Flask 检查 Host 标头,但这也没有改变它。
  • 嗯没关系,使用prox_set_header Host $host; 成功了!
  • 很高兴您找到了解决方案,代理有时需要特殊处理:flask.pocoo.org/docs/0.12/deploying/wsgi-standalone/…
猜你喜欢
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 2016-01-03
  • 1970-01-01
  • 2019-03-11
  • 2014-04-27
相关资源
最近更新 更多