【问题标题】:Flask : double slash in urls烧瓶:网址中的双斜杠
【发布时间】:2014-11-09 00:51:07
【问题描述】:

我在 gunicorn + nginx 下部署的 Flask 遇到了一些问题。我的网站在反向代理下可以访问 url:http://example.com/identity/

我的 Flask 应用程序中有几个 url,每个都有(例如“index”):

  • 当我尝试访问 http://example.com/identity/index/(带有斜杠)时,它会直接访问 url
  • 当我尝试访问http://example.com/identity/index(没有尾部斜杠)时,它转到http://example.com/identity//index/?(注意双斜杠和问号)。

在 Flask 中,与index 关联的路由是:@route('/identity/index')。我想我的问题是“正常”的 Flask 行为,但我想访问index 而不使用任何斜线并拥有我的正常网址。例如,使用redirect(url_for('index')) 进行重定向也是如此。

【问题讨论】:

  • 您看到的行为不是 Flask 所做的,也不是在本身不以 / 结尾的路由上。这是您的 nginx 配置、gunicorn 或您正在使用的一些中间件。
  • 感谢您的快速回答和格式化。我不使用任何中间件,所以我将看看我的 nginx 配置。我希望 gunicorn 没有修改我的网址...
  • 对于从谷歌登陆这里的人:Nginx 默认合并相邻的斜线。这可以通过merge_slashes off; 关闭,参见nginx.org/en/docs/http/ngx_http_core_module.html#merge_slashes

标签: python url flask slash


【解决方案1】:

实际上是我的 Nginx 配置有问题,我在外部运行 https,在服务器之间运行 http。所以我使用了这个 sn-p:http://flask.pocoo.org/snippets/35/ 在我的应用程序中强制使用 https url,现在它可以工作了!

【讨论】:

    猜你喜欢
    • 2019-11-23
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多