使用gunicorn和nginx处理错误请求

问题描述 投票:0回答:1

我收到“错误处理请求”,我无法真正追溯到一个特定的问题?我正在使用gunicorn + nginx,我的gunicorn设置是

gunicorn run:app -w 4 -b 0.0.0.0:8080 --workers=1 --timeout=300

这是错误消息

2015-10-14 21:27:11,287 DEBG 'myserver' stderr output:
[2015-10-14 21:27:11 +0000] [26725] [ERROR] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 130, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 171, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
   response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response

任何人都可以给我一个提示如何调试这个?我没有太多使用服务器的经验...谢谢卡尔

nginx gunicorn
1个回答
1
投票

tl; dr:这不是gunicorn或nginx的问题。 Flask应用中的视图功能未返回响应。当您收到此错误时,请检查视图函数中的return语句,以查找您正在访问的路径。

从线开始

Traceback (most recent call last):

您可以看到python解释器生成的堆栈跟踪。 stack trace显示嵌套函数的序列,直到您的代码失败。在我有限的经验中,python解释器堆栈跟踪可靠地将我引导到我的代码中的错误。

在你的情况下,最后一行:

ValueError: View function did not return a response

提供了一些关于错误的更多细节,并且应该让你很好地了解出了什么问题(你的视图函数没有返回响应)。

从底部开始的下一行将显示触发错误的函数及其在代码中的确切位置:

  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1566, in make_response
    raise ValueError('View function did not return a response')

在这种情况下,错误是由烧瓶源中的函数引发的,因此除非您对其进行了编辑,否则可能不是您需要进行修复的地方。基于跟踪末尾的特定ValueError,我将直接进入我的视图功能。在Flask中可能看起来像这样(例如来自Flask tutorial):

@app.route('/')
def show_entries():
    cur = g.db.execute('select title, text from entries order by id desc')
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
    return render_template('show_entries.html', entries=entries)

在你的情况下,最后一行似乎是一个好的开始,因为错误说没有返回任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.