23

我有一个通过 Nginx 和 uWsgi 托管的 django 应用程序。在某个非常简单的请求中,我得到了 GET 和 POST 的不同行为,这不应该是这种情况。

uWsgi 守护进程日志:

[pid: 32454|app: 0|req: 5/17] 127.0.0.1 () {36 vars in 636 bytes} [Tue Oct 19 11:18:36 2010] POST /buy/76d4f520ae82e1dfd35564aed64a885b/a_2/10/ => generated 80 bytes in 3 msecs (HTTP/1.0 440) 1 headers in 76 bytes (0 async switches on async core 0)
[pid: 32455|app: 0|req: 5/18] 127.0.0.1 () {32 vars in 521 bytes} [Tue Oct 19 11:18:50 2010] GET /buy/76d4f520ae82e1dfd35564aed64a885b/a_2/10/ => generated 80 bytes in 3 msecs (HTTP/1.0 440) 1 headers in 76 bytes (0 async switches on async core 0)

Nginx 访问日志:

127.0.0.1 - - [19/Oct/2010:18:18:36 +0200] "POST /buy/76d4f520ae82e1dfd35564aed64a885b/a_2/10/ HTTP/1.0" 440 0 "-" "curl/7.19.5 (i486-pc-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.15"
127.0.0.1 - - [19/Oct/2010:18:18:50 +0200] "GET /buy/76d4f520ae82e1dfd35564aed64a885b/a_2/10/ HTTP/1.0" 440 80 "-" "curl/7.19.5 (i486-pc-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.15"

Nginx 错误日志:

2010/10/19 18:18:36 [error] 4615#0: *5 readv() failed (104: Connection reset by peer) while reading upstream, client: 127.0.0.1, server: localhost, request: "POST /buy/76d4f520ae82e1dfd35564aed64a885b/a_2/10/ HTTP/1.0", upstream: "uwsgi://unix:sock/uwsgi.sock:", host: "localhost:9201"

本质上,如果我使用 POST,Nginx 会在某处丢失响应,如果我使用 GET,则不会。

有人知道吗?

4

4 回答 4

28

传递--post-buffering 1给 uwsgi

这将自动缓冲所有 > 1 字节的 http 正文

问题是由 nginx 管理上游断开连接的方式引起的

于 2011-02-25T06:58:46.157 回答
5

我遇到了同样的问题,但在我的情况下,我无法禁用“uwsgi_pass_request_body”,因为大多数时候(但并非总是)我的应用确实需要 POST 数据。

这是我找到的解决方法,虽然这个问题在 uwsgi 中没有解决:http: //permalink.gmane.org/gmane.comp.python.wsgi.uwsgi.general/813

import django.core.handlers.wsgi
class ForcePostHandler(django.core.handlers.wsgi.WSGIHandler):
    """Workaround for: http://lists.unbit.it/pipermail/uwsgi/2011-February/001395.html
    """
    def get_response(self, request):
        request.POST # force reading of POST data
        return super(ForcePostHandler, self).get_response(request)

application = ForcePostHandler()
于 2011-02-18T15:58:32.417 回答
5

我面临同样的问题。我尝试了上述所有解决方案,但它们都不起作用。在我的情况下,忽略响应正文根本不是一种选择。

显然,在处理响应小于 4052 字节的 POST 请求时,这是 nginx 和 uwsgi的错误

为我解决的问题是将“--pep3333-input”添加到uwsgi的参数列表中。之后,所有 POST 都会正确返回。

我正在使用的 nginx/uwsgi 版本:

$ nginx -V
nginx: nginx version: nginx/0.9.6

$ uwsgi --version
uWSGI 0.9.7
于 2011-06-12T06:05:44.477 回答
1

在进一步研究中幸运地发现 (http://answerpot.com/showthread.php?577619-Several%20Bugs/Page2) 我发现了一些有用的东西......

在 Nginx conf 中提供uwsgi_pass_request_body off;参数可以解决这个问题......

于 2010-10-19T16:36:25.143 回答