【问题标题】:Python Flask server crashing on GET request to specific endpoint:Python Flask 服务器在对特定端点的 GET 请求时崩溃:
【发布时间】:2013-12-26 20:26:33
【问题描述】:

全部,

我有一个使用 Flask 构建的带有两个端点的 API。我正在使用 nginx/uwsgi 组合进行服务,每当我向其中一个端点发送 GET 请求时,我都会收到一个奇怪的错误。另一个端点工作得很好。

这是我的 uwsgi 日志从获取请求中得到的输出

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1646, in request_context
    return RequestContext(self, environ)
File "/usr/local/lib/python2.7/dist-packages/flask/ctx.py", line 166, in __init__
    self.url_adapter = app.create_url_adapter(self.request)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in create_url_adapter
    server_name=self.config['SERVER_NAME'])
File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1196, in bind_to_environ
    environ['REQUEST_METHOD'], environ.get('PATH_INFO'),
KeyError: 'REQUEST_METHOD'

还有,这里是 nginx 错误日志中的一个 get 请求的输出

2013/12/26 15:22:16 [error] 833#0: *9 upstream prematurely closed connection while reading response header from upstream, 
client: 71.71.53.31, server: scholarly, 
request: "GET /citelet/ HTTP/1.1", 
upstream: "uwsgi://unix:///tmp/citelet.sock:", 
host: "162.243.219.38"

对于这个问题含糊不清,我深表歉意。我已经在相同的硬件上使用相同的库多次设置此服务器,并且之前没有任何问题。该错误令人困惑,我不确定从哪里开始查找。

提前致谢!

【问题讨论】:

  • 你能给你看看 uwsgi 应用程序的 nginx.conf 部分吗?您的 python 应用程序代码也会有所帮助。我遇到了这个,但你的其他端点工作没有意义......en.it-usenet.org/thread/17304/5165/#post5163
  • @AndrewKloos 这是一个愚蠢的错误,几乎总是如此。我的 nginx 配置中有一个字母类型。我将在答案中发布配置。不过感谢我们的回复!

标签: python nginx flask uwsgi


【解决方案1】:

愚蠢的错误。我的 nginx 配置中有错误。它指向一个不存在的套接字。

server {
    listen   80;
          server_name scholarly;
      # crowdscholar endpoint
        location /crowdscholar {
            uwsgi_pass unix:///tmp/crowdscholar.sock;
        include uwsgi_params;
        # strip path before handing it to app
        uwsgi_param SCRIPT_NAME /crowdscholar;
        uwsgi_modifier1 30;
    }
    # citelet endpoint
        location /citelet {
            uwsgi_pass unix:///tmp/citelet.sock;
        include uwsgi_params;
        # strip path before handing it to app
        uwsgi_param SCRIPT_NAME /citelet;
        uwsgi_modifier1 30;
    }
}

【讨论】:

  • 感谢您发布有效的配置;它帮助我解决了我的问题,最多只有一个 API 响应。看起来对我来说重要的部分是使用三个正斜杠而不是一个。
【解决方案2】:

仅供参考:我遇到了同样的错误,并意识到我忘记在user1558914 的答案中添加include uwsgi_params;

然后在make restart 之后它仍然无法正常工作,因为我的 uwsgi 重启规则在 nginx 重启之前失败了。一旦我使用/etc/init.d/nginx restart 手动重启 nginx,KeyError 就停止了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 2016-11-14
    • 2015-07-23
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多