Nginx/wsgi部署Django:避开502KeyError: u'REQUEST_METHOD'错误

Nginx/wsgi部署Django会有各种坑,主要是一些教程根本就不完整,都是抄过来的,所以按照教程就会遇到很多坑,最常见的就是502的坑,下面是我在部署的过程中,几个重要的文件,我就按照网上的教程操作,没有避开502KeyError: u'REQUEST_METHOD'错误,导致浪费了很多时间。

django项目目录结构

django文件结构

uwsgi.ini文件代码:

#mysite_uwsgi.ini file

[uwsgi]

# Django-related settings

# the base directory (full path)

chdir = /var/www/yt

# Django's wsgi file

module = yt.wsgi

# the virtualenv (full path)

# process-related settings

# master

master = true

# maximum number of worker processes

processes = 4

# the socket (use the full path to be safe

socket = 127.0.0.1:8888

# ... with appropriate permissions - may be needed

chmod-socket = 666

# clear environment on exit

vacuum = true

socket-timeout = 600

buffer-size = 65535

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 UWSGI_SCHEME $scheme;

uwsgi_param REMOTE_ADDR $remote_addr;

uwsgi_param REMOTE_PORT $remote_port;

uwsgi_param SERVER_PORT $server_port;

uwsgi_param SERVER_NAME $server_name;

这里一定要注意的是这里是uwsgi_param 不要搞成wsgi_param了,甚至有的教程,就是wsgi_param,最终会报502,我就是在这里被坑的,还是从github上下载的uwsgi_params这个文件,就是wsgi_param,所以坑死人不偿命。

如果你不幸下载了某个教程上叫你到github上下载的是wsgi_param,就会报下面的错误:

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 167, in __call__

request = self.request_class(environ)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 97, in __init__

self.method = environ['REQUEST_METHOD'].upper()

KeyError: u'REQUEST_METHOD'

还有一个是nginx的配置文件,放在/etc/nginx/conf.d/目录下:命名为yt.conf

# the upstream component nginx needs to connect to

upstream django {

#server unix:/var/www/yt/yt.sock; # for a file socket

server 127.0.0.1:8888; # for a web port socket (we'll use this first)

}

# configuration of the server

server {

# the port your site will be served on

listen 80;

# the domain name it will serve for

server_name 192.168.0.5; # substitute your machine's IP address or FQDN

charset utf-8;

# max upload size

client_max_body_size 75M; # adjust to taste

# Django media

location /media {

alias /var/www/yt/media; # your Django project's media files - amend as required

}

location /static {

alias /var/www/yt/static; # your Django project's static files - amend as required

}

# Finally, send all non-media requests to the Django server.

location / {

uwsgi_pass django;

uwsgi_send_timeout 600; # 指定连接到后端uWSGI的超时时间。

uwsgi_connect_timeout 600; # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。

uwsgi_read_timeout 600;

keepalive_timeout 650;

include /var/www/yt/uwsgi_params; # the uwsgi_params file you installed

proxy_buffers 8 32k;

proxy_buffer_size 64k;

}

}

上面就是django部署的重要文件。

以上是 Nginx/wsgi部署Django:避开502KeyError: u'REQUEST_METHOD'错误 的全部内容, 来源链接: utcz.com/a/831.html

回到顶部