mrisher mrisher - 1 year ago 220
Python Question

Debugging Apache/Django/WSGI Bad Request (400) Error

My simple Django app worked fine in debug mode ( runserver
), and works under WSGI+Apache on my dev box, but when I pushed to EC2 I began receiving intermittent (10-80% of the time) errors of
Bad Request (400)
for any URLs I try to view (whether in my app or in the Django admin.

Where can I find debug information about this? Nothing appears in
, even with
. I have checked versions, logged the Request environment (cf. ModWSGI Debugging Tips) and see no major differences.

The one remaining thought I had is, I'm using the mod_wsgi from Ubuntu 12.04 (libapache2-mod-wsgi 3.3-4build1) which was built against Python 2.7.1; I have Python 2.7.3. And Django is 1.6, which is newer than the Ubuntu Precise version. I hesitate to start building packages from source since it's so hard to clean up and these seem like minor version changes...

Thank you for your help.

(For reference, here are the Apache config and WSGI apps)

Apache config (000-default)

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
WSGIScriptAlias /rz /usr/local/share/rz/rz.wsgi

rz.WSGI app

import os
import sys
import django.core.handlers.wsgi
import pprint

path = '/usr/local/share/rz'
if path not in sys.path:
sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'rz.settings'

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(django.core.handlers.wsgi.WSGIHandler())

Answer Source

Add the ALLOWED_HOSTS setting to your like so...

    '', # Allow domain and subdomains
    '', # Also allow FQDN and subdomains

I had this same problem and found the answer here in the docs

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download