Debugging Apache / Django / WSGI error request (400)

My simple Django application works fine in debug mode ( manage.py runserver ) and running WSGI + Apache on my dev box, but when I clicked on EC2, I started getting intermittent (10-80% of the time) Error Bad Request (400) for all URL-addresses, which I try to browse (be it an application or administrator Django.

Where can I find debugging information about this? Nothing is displayed in /var/log/apache2/error.log , even with LogLevel=info . I checked the version, registered the request on Wednesday (see. Debugging Tips ModWSGI ) and do not see significant differences.

The only remaining I find that I use mod_wsgi of Ubuntu 12.04 (libapache2-mod-wsgi 3.3-4build1), which was established against Python 2.7.1; In Python 2.7.3 me. And Django is 1.6, newer than the version of Ubuntu Precise. I hesitate to start building packages from source code, since it is so difficult to clean, and it looks like a minor change in the version of ...

Thank you for your help.

(For reference, here are the Apache and WSGI application settings)

Apache configuration (000 by default)

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

Appendix rz.WSGI

 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()) rz.settings' 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()) : 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()) start_response): 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()) ), stream = errors) 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()) args): 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()) , headers), stream = errors) 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()) args) 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()) ) 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()) 
+58
python django apache mod-wsgi django-wsgi
December 2. '13 at 5:49
source share
3 answers

Add the ALLOWED_HOSTS setting to your settings.py parameters, for example ...

 ALLOWED_HOSTS = [ '.example.com', # Allow domain and subdomains '.example.com.', # Also allow FQDN and subdomains ] subdomains ALLOWED_HOSTS = [ '.example.com', # Allow domain and subdomains '.example.com.', # Also allow FQDN and subdomains ] FQDN and subdomains ALLOWED_HOSTS = [ '.example.com', # Allow domain and subdomains '.example.com.', # Also allow FQDN and subdomains ] 

I had the same problem and found the answer here, in documents

update: django 1.6 docs no longer on the network, I have updated the link to go to the django 1.7 docs for ALLOWED_HOSTS .

+98
December 16. '13 at 19:03
source share

If you definitely established ALOWED_HOSTS - make sure that your host name contains an underscore character. It is technically illegal.

I had to print out a variety of functions, and it came down to this regular expression, not finding domain django.http

 host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$") 

Indeed, my domain had an underscore in it.

+7
Nov 04. '14 at 8:35
source share

This is not a solution, but you can set for debugging purposes ALLOWED_HOSTS setting in their preferences. How to do it

 ALLOWED_HOSTS = ['*'] 

It definitely should work. If not, at least you will know that the problem is not in the Django, deprived of access to this URL.

0
December 16. '14 at 10:04
source share



All Articles