self.method = environ['REQUEST_METHOD'].upper() KeyError: 'REQUEST_METHOD' while setting up django server on EC2 using uwsgi

self.method = environ['REQUEST_METHOD'].upper() KeyError: 'REQUEST_METHOD' while setting up django server on EC2 using uwsgi

我得到一个

当我点击 http://ec2-X-YZ-ABC-EFG.compute-1.amazonaws.com/admin 时。

我很确定我的 Django 应用程序没有任何问题,因为该应用程序是空的。我刚刚使用 django-admin startproject tempo 创建了这个应用程序,只是做了这个更改--ALLOWED_HOSTS = ['*'],这样它就可以接受来自任何 IP 的请求。

因为这是一个开发服务器并且不包含任何数据,所以我允许来自任何主机的请求。这是我的入站规则,

Ports   Protocol    Source
 80       tcp      0.0.0.0/0, ::/0
 22       tcp      0.0.0.0/0, ::/0  
 443      tcp      0.0.0.0/0, ::/0

这是我的 /etc/nginx/sites-enabled/tempo

upstream tempo-server {
    server   unix:///home/ubuntu/tempo/tempo.sock;
}
server {
    error_log       /var/log/nginx/tempo/error.log;
    access_log      /var/log/nginx/tempo/access.log;
    listen 80;
    server_name X.YX.ABC.DEF;
    location = /media/ {
        root /home/ubuntu/tempo/media;
    }
    location / {
        include        /etc/uwsgi/sites/uwsgi_params;
        uwsgi_pass    tempo-server;
    }
}

这是我的 uwsgi.ini 文件

[uwsgi]
chdir           = /home/ubuntu/tempo
module          = tempo.wsgi
home            = /home/ubuntu/seatr/venv-seatr
master          = true
processes       = 10
socket          = /home/ubuntu/tempo/tempo.sock
chmod-socket    = 777
vacuum          = true

另外,uwsgi启动没有报错,.sock文件也创建成功。

nginx error.log 显示:

2019/04/04 20:03:48 [error] 30261#30261: *9 upstream prematurely closed connection while reading response header from upstream, client: 129.219.8.129, server: X.YZ.ABC.DEF, request: "GET /admin HTTP/1.1", upstream: "uwsgi://unix:///home/ubuntu/tempo/tempo.sock:", host: "ec2-X-YZ-ABC-DEF.compute-1.amazonaws.com"

PS:我的启用站点(软链接)仅包含此 tempo 文件,可用站点包含几个文件。

我读过这个问题在多个地方被问到,但没有答案:

here here

我的uwsgi_params文件完全是空的。结果 nginx 无法发送像“REQUEST_METHOD”这样的参数,因此出现了上述错误。

我的uwsgi_params文件如下:

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;