4

I'm trying to lunch my app on VPS in Debug=False mode. Debug=True works fine but when I change it to false I got this error. I'm using Apache for rendering python pages and Nginx to serve my static files. I tried using this [answer]: Debugging Apache/Django/WSGI Bad Request (400) Error but it's not working at least for me. And this is my wsgi config:

#wsgi.py
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
path = '/var/www/example'
if path not in sys.path:
    sys.path.append(path)

And also I've added below code to my settings file:

ALLOWED_HOSTS = [
    '.example.com', # Allow domain and subdomains
    '.example.com.', # Also allow FQDN and subdomains
]
Community
  • 1
  • 1
hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • 1
    And you replace the `example.com` with your real domain name? did you get 404 error for this `ALLOWED_HOSTS = '*'` also? do you have `404.html` template file in your root of template folder? – Omid Raha Feb 19 '14 at 22:32
  • @OmidRaha Thanks I changed the `ALLOWED_HOSTS = '*'` and the problem solved. Can you explain why my domain not woks? And yes I change example.com to my real domain name. – hamidfzm Feb 19 '14 at 22:37
  • The `*` is not good, fix it by adding your domain(s), looks at [ALLOWED_HOSTS](https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts) – Omid Raha Feb 19 '14 at 22:43
  • No, both don't work. But what is the `*` problem? – hamidfzm Feb 19 '14 at 23:08
  • 1
    The `*` has security [issue](https://docs.djangoproject.com/en/1.6/ref/settings/#allowed-hosts), first set `*` for `ALLOWED_HOSTS` and then in somewhere in view print and see output of `print(request.META['HTTP_HOST'])` or `print(request.get_host())`, then set that output (just domain of it as list) to your `ALLOWED_HOSTS` – Omid Raha Feb 19 '14 at 23:54
  • @OmidRaha I did what you said and I found the problem. I use `proxy_pass` in nginx to to goto 127.0.0.1:8000 so my host name was that! Please make your comments as an answer. – hamidfzm Feb 20 '14 at 00:24

2 Answers2

20

To discover your problem, first in settings.py set ALLOWED_HOSTS temporarily to:

ALLOWED_HOSTS = '*'

And then in somewhere in your view, try to print out and see output of this command:

print(request.META['HTTP_HOST']) # or print(request.get_host())

Then according to output, set that (just domain of it as an list) to your ALLOWED_HOSTS.

Notes:

  • Use ALLOWED_HOSTS = '*' may have security issue for you, read about that here.

  • After every change you need to restart your service(apache/nginx).

Omid Raha
  • 9,862
  • 1
  • 60
  • 64
0

I've had the same problem with my cyrillic domain.

I've added punicode representation of my domain to ALLOWED_HOSTS, and that solved the problem.

Maybe this should be sent as a bug to Django, but I didn't dig much into that (it could be an issue of my hosting configuration), just ab (and of course browser). The problem was also intermittent for me (usually worse than what is shown below with ab).

Without punicode

Complete requests: 20 Failed requests: 3

while with punicode

Complete requests: 20 Failed requests: 0

Yaroslav Nikitenko
  • 1,695
  • 2
  • 23
  • 31