Django uWSGI NGINX Bad Request 400

doh*_*utz 9 django nginx uwsgi

在尝试使用django开发服务器部署我开发的博客时收到400错误后,我启动了一个新的测试项目(使用startproject并且什么都不做 - 只需要一点点配置) - 尽量少尽可能保持简单.

当我执行"manage.py runserver"时,它会显示一个页面,说我看到了这个,因为我的设置中有"DEBUG = True".

到现在为止还挺好.没有错误.

但是,如果我使用uWSGI和NGINX,我再次收到"错误请求(400)"页面.

最初我有一些导入错误,我不得不添加一些sys.path路径.但是现在我没有从python,NGINX或uWSGI那里得到任何错误,并且仍然以400-Error-page结束.

我尝试过以下方法:

  • DEBUG = False
  • TEMPLATE_DEBUG = False
  • ALLOWED_HOSTS = ['*']
  • ALLOWED_HOSTS ='*'
  • 从MIDDLEWARE_CLASSES中删除'django.middleware.clickjacking.XFrameOptionsMiddleware'
  • 使用带有uWSGI的NGINX代替带有mod_wsgi的Apache(我坚持使用此设置,因为我喜欢它,但这并没有解决我的问题)

我的设置:uWSGI,NGINX和客户端(firefox)从我的笔记本(kubuntu 14.04)中运行.Vhost/subdomain(cefk_blawg.localhost),它位于hosts文件(cefk_blawg.localhost 127.0.0.1)中并在NGINX中正确配置(我知道,因为当我使用金字塔测试项目时,它实际上就像一个魅力).路上没有防火墙.使用virtualenv和pip安装其中的所有内容(django/uwsgi/pillow/mysql-python).

我的uwsgi.ini:

[uwsgi]

# Unix socket (full path)
socket = /tmp/cefk_blawg.sock

# Set socket permissions
chmod-socket = 666

# Master process
master = true

# Maximum number of worker processes
processes = 4

# Set timeout
harakiri = 60
harakiri-verbose = true

# Limit post-size
limit-post = 65536

# When to start buffering for post-vars
post-buffering = 1       ## none of these makes my problem go away
#post-buffering = 8192   ## none of these makes my problem go away
#post-buffering = 32768  ## none of these makes my problem go away

# Daemonize
daemonize = /home/cefk/Dokumente/cefk_blawg/uwsgi.log
pidfile = /home/cefk/Dokumente/cefk_blawg/uwsgi.pid

# Limit queue
listen = 64
max-requests = 1000

# Whatever this does .. it works for pyramid (got it from a tutorial)
reload-on-as = 128
reload-on-rss = 96

no-orphans = true
log-slow = true

# This is the full path to my virtualenv
virtualenv = /home/cefk/Dokumente/cefk_blawg/venv

# Django wsgi file
wsgi-file = /home/cefk/Dokumente/cefk_blawg/cefk_info/cefk_info/wsgi.py

# Settings file (this seems to do nothing)
# And it gets set in the wsgi.py-file
env = DJANGO_SETTINGS_MODULE=cefk_info.settings

# Set domain (this seems to do nothing)
#domain = cefk_blawg.localhost

# Django-project base directory (this seems to do nothing)
#chdir = /home/cefk/Dokumente/cefk_blawg/cefk_info

# This seems to do nothing
#pythonpath=/home/cefk/Dokumente/cefk_blawg/cefk_info/cefk_info/

# Set vhost (this seems to do nothing)
#vhost = true

# Clean up environment on exit
vacuum = true
Run Code Online (Sandbox Code Playgroud) #

我的wsgi.py文件:

import os
import pprint
import site
import sys
from django.core.wsgi import get_wsgi_application

base_parent = '/home/cefk/Dokumente/cefk_blawg/'
base = '/home/cefk/Dokumente/cefk_blawg/cefk_info/'

sys.path.append(base_parent)
sys.path.append(base)

site.addsitedir(
    '/home/cefk/Dokumente/cefk_blawg/venv/local/lib/python2.7/site-packages'
)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cefk_info.settings")

activate_env = '/home/cefk/Dokumente/cefk_blawg/venv/bin/activate_this.py'
execfile(activate_env, dict(__file__=activate_env))

# I stole this shamelessly from another stackoverflow-post - this is good to have
class LoggingMiddleware:
    def __init__(self, application):
        self.__application = application

    def __call__(self, environ, start_response):
        errors = environ['wsgi.errors']
        pprint.pprint(('REQUEST', environ), stream=errors)

        def _start_response(status, headers, *args):
            pprint.pprint(('RESPONSE', status, headers), stream=errors)
            return start_response(status, headers, *args)

        return self.__application(environ, _start_response)

application = LoggingMiddleware(get_wsgi_application())
Run Code Online (Sandbox Code Playgroud)

这是我的请求/响应,我从wsgi.py中的LoggingMiddleware获取:

(
    'REQUEST',
    {
        'CONTENT_LENGTH': '',
        'CONTENT_TYPE': '',
        'DOCUMENT_ROOT': '/home/cefk/Dokumente/cefk_blawg/cefk_info/cefk_info',
        'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
        'HTTP_ACCEPT_LANGUAGE': 'de,en-US;q=0.7,en;q=0.3',
        'HTTP_CACHE_CONTROL': 'max-age=0',
        'HTTP_CONNECTION': 'keep-alive',
        'HTTP_DNT': '1',
        'HTTP_HOST': 'cefk_blawg.localhost',
        'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0',
        'PATH_INFO': '/',
        'QUERY_STRING': '',
        'REMOTE_ADDR': '127.0.0.1',
        'REMOTE_PORT': '42518',
        'REQUEST_METHOD': 'GET',
        'REQUEST_URI': '/',
        'SERVER_NAME': 'cefk_blawg.localhost',
        'SERVER_PORT': '80',
        'SERVER_PROTOCOL': 'HTTP/1.1',
        'UWSGI_SCHEME': 'http',
        'uwsgi.node': 'lt',
        'uwsgi.version': '2.0.5.1',
        'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0x7ff4337110c0>,
        'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
        'wsgi.input': <uwsgi._Input object at 0x7ff437271e70>,
        'wsgi.multiprocess': True,
        'wsgi.multithread': False,
        'wsgi.run_once': False,
        'wsgi.url_scheme': 'http',
        'wsgi.version': (1, 0)
    }
)
('RESPONSE', '400 BAD REQUEST', [('Content-Type', 'text/html')])
[pid: 2652|app: 0|req: 1/1] 127.0.0.1 () {42 vars in 675 bytes} [Thu Jun 12 17:16:59 2014] GET / => generated 26 bytes in 150 msecs (HTTP/1.1 400) 1 headers in 53 bytes (1 switches on core 0)
Run Code Online (Sandbox Code Playgroud)

编辑:这是我的nginx-config(注意,文件夹名称可能在此期间发生了变化 - 所以请忽略它):

# Server configuration
server {
    # Make site accessible from http://cefk_blawg.localhost/
    server_name cefk_blawg.localhost;

    root /home/cefk/Dokumente/cefk_blawg_django;

    # Set charset
    charset utf-8;
    client_max_body_size 100M;

    location /static {
        autoindex on;
        alias /home/cefk/Dokumente/cefk_blawg_django/static;
    }

    location /media {
        autoindex on;
        alias /home/cefk/Dokumente/cefk_blawg_django/media;
    }

    ################################
    # Port-based (old)             #
    ################################
    #location / {
    #    try_files $uri @application;
    #}  

    #location @application {
    #    include /etc/nginx/uwsgi_params;
    #    uwsgi_pass 127.0.0.1:8000;
    #}
    ################################
    # /Port-based (old)            #
    ################################

    location / {
        include /etc/nginx/uwsgi_params;
        uwsgi_pass unix:///tmp/cefk_blawg.sock;
    }
}
Run Code Online (Sandbox Code Playgroud)

/编辑

我没有想法.

请帮忙.

Ale*_*lex 8

您可以DEBUG = True在服务器上进行设置,重新启动uwsgi服务并django在浏览器中检查调试输出.事实上,你没有看到任何错误django的发展服务器并不意味着错误与nginxuwsgi服务.


Ase*_*eem 5

在settings.py中

ALLOWED_HOSTS = ['*']
Run Code Online (Sandbox Code Playgroud)

会解决


doh*_*utz 5

自从我五年前问过这个问题以来,我可能是错的。但我现在将尽我所能回答这个问题,因为所有出现的答案似乎与我的解决方案相去甚远。

我最近在几个项目中再次使用 Django 并遇到了类似的问题。

我做错的事情清单:

  • 在 virtualenv 以及系统全局中安装了 uwsgi
  • uwsgi-config 中应用程序的路径错误
  • uwsgi-config 中 virtualenv 的路径错误
  • 用于连接 uwsgi 和 nginx 的套接字文件的权限设置不正确

这就是我能想到的,我以前做错的事情。

我希望这对所有因设置而苦苦挣扎的人有所帮助。