【问题标题】:404 Not Found error using Gunicorn/nginx only仅使用 Gunicorn/nginx 时出现 404 Not Found 错误
【发布时间】:2021-07-20 07:42:49
【问题描述】:

我有一个烧瓶应用程序,我通过 gunicorn 和 nginx 在 Ubuntu 18.04 服务器上运行。我有几个路由文件,都导入到__init__.py,而且通常工作得很好。我为其中一个文件添加了一条新路线,基本上,就像所有其他文件一样。该路由在我的开发机器上本地运行良好,但是当我在服务器上运行相同的代码时,我收到 404 错误。

我完全被难住了。

这是一个不起作用的路由文件的 sn-p:

# This route works fine

@app.route('/admin/monthy_bad')
@login_required
def monthly_bad():
    monthly_bad = admin_utilities.monthly_bad(current_user.admin, current_user.domain_group_id)
    return render_template('monthly_bad.html', name=current_user.name, monthly_bad=monthly_bad)

# This route fails with 404 on the server, but not locally

@app.route('/domain_group/admin')
@login_required
def domain_group_admin():
    """
    Admin page for domain groups
    """
    no_domain_group = DomainGroup.query.filter_by(name='None').first_or_404()
    if current_user.domain_group_id == str(no_domain_group.id): # bump them
        flash("No Domains!")
        return redirect(url_for('profile'))
    else:
        return render_template('domain_group_admin.html', name=current_user.name, domain_group_id=current_user.domain_group_id)

我认为这可能是缓存问题 - 但我已经多次重新启动应用程序(甚至重新启动服务器。)

任何关于在哪里进行故障排除的想法都会非常有帮助。

【问题讨论】:

  • 服务器应该在日志文件中保存一些消息,您应该首先检查它 - 在那里您应该获得更多信息可能是什么问题。
  • 您使用first_or_404(),当它在数据库中找不到数据时可能会生成404 - 所以您应该检查服务器上的数据库中是否有正确的数据。您还可以使用logging 模块来保护日志文件中的信息 - 这样您就可以检查从数据库中获得的信息。您还可以将问题拆分为更小的问题 - 首先仅使用 return "Hello World" 运行此函数以检查它是否已执行。

标签: python nginx flask gunicorn


【解决方案1】:

furas 有正确的答案,这正是问题所在!我需要的数据在服务器上丢失了,first_or_404() 返回了 404 错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2017-09-03
    • 2012-09-27
    • 2014-08-01
    • 2017-11-25
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多