Why am I getting an apache server error page on a django vhost using underscore in my subdomain?
Question: I'm using django 1.5.0 and apache 2.2.22. I can't seem to get my site running
as a named vhost (I've got other django sites already running fine on this
server).

I'm getting the standard apache 500 error page. My error log suggests that've
I've got a problem with my ALLOWED_HOSTS setting -

    
    
    [Tue Jun 04 10:25:22 2013] [error] [client 31.52.39.247] SuspiciousOperation: Invalid HTTP_HOST header (you may need to set ALLOWED_HOSTS): sub_domain.mydomain.com
    

But my `ALLOWED_HOSTS` setting looks ok to me -

    
    
    python manage.py shell
    >> from django.conf import settings
    >> settings.ALLOWED_HOSTS
    ['sub_domain.mydomain.com', 'livedomain.com']
    

Also - I don't know why I'm getting the apache 500 page rather than the django
debug page -

    
    
    >> settings.DEBUG
    True
    

Looking at the full stack trace in the error log, I can see that the wsgi app
is running - it's definitely running django code.

Any suggestions?


Answer: Firstly, `ALLOWED_HOSTS` should be ignored altogether when you have
`DEBUG=True` so that's strange that you are getting validation errors.

I was having an issue similar to this with nginx where I had an underscore in
my host name which meant Django didn't validate it. This might be an issue
particularly if you are having Apache rewriting your host header (this was the
case for me as nginx was reverse proxying) or your subdomain includes an `_`.

[You can see how Django validates hostnames in the
source](https://github.com/django/django/blob/master/django/http/request.py#L27),
furthermore, [I wrote a quick blog post on
this](http://timmyomahony.com/blog/2013/04/24/suspiciousoperation-invalid-
http_host-header-django/) also which might help

