NGINX + uWSGI连接由Peer重置

Kar*_*kde 9 nginx bottle uwsgi

我正在尝试使用uWSGI在NGINX上托管Bottle Application.

这是我的nginx.conf

location /myapp/ {
        include uwsgi_params;
        uwsgi_param X-Real-IP $remote_addr;
        uwsgi_param Host $http_host;
        uwsgi_param UWSGI_SCRIPT myapp;
        uwsgi_pass 127.0.0.1:8080;
    }
Run Code Online (Sandbox Code Playgroud)

我正在运行uwsgi

uwsgi --enable-threads --socket :8080 --plugin python -- wsgi-file ./myApp/myapp.py
Run Code Online (Sandbox Code Playgroud)

我正在使用POST请求.对于使用dev Http Client的那个.当我发送请求时,这是无限的

http://localhost/myapp
Run Code Online (Sandbox Code Playgroud)

uWSGI服务器接收请求并打印

[pid: 4683|app: 0|req: 1/1] 127.0.0.1 () {50 vars in 806 bytes} [Thu Oct 25 12:29:36 2012] POST /myapp => generated 737 bytes in 11 msecs (HTTP/1.1 404) 2 headers in 87 bytes (1 switches on core 0)
Run Code Online (Sandbox Code Playgroud)

但在nginx错误日志中

2012/10/25 12:20:16 [error] 4364#0: *11 readv() failed (104: Connection reset by peer) while reading upstream, client: 127.0.0.1, server: localhost, request: "POST /myApp/myapp/ HTTP/1.1", upstream: "uwsgi://127.0.0.1:8080", host: "localhost"
Run Code Online (Sandbox Code Playgroud)

该怎么办?

har*_*rmv 8

确保在您的应用程序中使用您的帖子数据

例如,如果你有一个python应用程序

def my_view(request):

    # ensure to read the post data, even if you don't need it
    # without this you get a: failed (104: Connection reset by peer)
    data = request.DATA

    return HttpResponse("Hello World")
Run Code Online (Sandbox Code Playgroud)

  • 对于Django/UWSGI/Nginx堆栈,这也是我的问题.我不知道这是件事.这有什么文件/资源​​吗?为什么我必须使用POST数据?编辑:这里有一些细节(尽管很模糊):http://uwsgi-docs.readthedocs.org/en/latest/ThingsToKnow.html (2认同)

rob*_*rto 7

如果不在应用程序中读取数据,则无法从客户端发布数据.虽然这在uWSGI中不是问题,但nginx会失败.您可以使用uWSGI的--post-buffering选项"伪造"该东西,以自动从套接字读取数据(如果可用),但您最好"修复"(即使我不认为这是一个错误)你的应用

  • 你在你的应用程序中使用发布数据? (2认同)

rkr*_*rzr 5

当请求的主体不被使用时,会出现此问题,因为uwsgi无法确定在某些时候是否仍然需要它。因此,uwsgi将继续保留数据,直到数据被消耗或nginx重置连接为止(由于上游超时)。

uwsgi的作者在这里对此进行解释:

08:21 < unbit> plaes: does your DELETE request (not-response) have a body ?
08:40 < unbit> and do you read that body in your app ?
08:41 < unbit> from the nginx logs it looks like it has a body and you are not reading it in the app
08:43 < plaes> so DELETE request shouldn't have the body?
08:43 < unbit> no i mean if a request has a body you have to read/consume it
08:44 < unbit> otherwise the socket will be clobbered
Run Code Online (Sandbox Code Playgroud)

因此,要解决此问题,您需要确保始终读取整个请求正文,或者在不需要时不发送正文(例如,对于DELETE)。