Django Bad Request(400) Error in Deployment with Apache/NginX Django Bad Request(400) Error in Deployment with Apache/NginX nginx nginx

Django Bad Request(400) Error in Deployment with Apache/NginX


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, readabout that here.

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


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: 20Failed requests: 3

while with punicode

Complete requests: 20Failed requests: 0