Skip to content

Instantly share code, notes, and snippets.

@eezis
Last active June 28, 2023 16:23
Show Gist options
  • Save eezis/7586964 to your computer and use it in GitHub Desktop.
Save eezis/7586964 to your computer and use it in GitHub Desktop.
Resolving Bad Request (400) when using Django on an EC2 instance DEBUG
This gist pertains to a Bad Request(400) error when trying to access an EC2 instance running django . . .
I set up an EC2 server, installed django, started runserver and tested. The result was
Bad Request (400)
When using Django 1.5 and higher, if you specify DEBUG=False in the settings.py file, you must also specify ALLOWED_HOSTS
or you will recieve a "Bad Request (400)" error message when attempting to retrieve a page.
On an Amazone EC2 instance, if you are ***testing*** with the public DNS, then you should include the public DNS as one of the allowed hosts.
--- example ---
DEBUG = False
ALLOWED_HOSTS = ['.yourdomain.com', 'ec2-XX-XX-XX-XXX.us-west-2.compute.amazonaws.com',]
--- end ---
If have SSH'ed into the server and wish to perform on-server testing you should include 'localhost' as well:
python manage.py runserver 0:8000
then . . .
curl http://localhost:8000/
or . . .
telnet localhost 8000 (then type GET / and hit enter twice)
If that does not work add 'localhost' to your ALLOWED_HOSTS file.
ALLOWED_HOSTS = ['.yourdomain.com', 'ec2-XX-XX-XX-XXX.us-west-2.compute.amazonaws.com', 'localhost',]
========
NOTE WELL: If you reboot your EC2 instance, when it reboots it will have a different public DNS and you
will need to update your ALLOWED_HOSTS accordingly.
See the 1.5 release notes for more:
https://docs.djangoproject.com/en/dev/releases/1.5/#allowed-hosts-required-in-production
@holms
Copy link

holms commented Apr 15, 2015

even with ALLOWED_HOSTS = ['*'] i've got Bad Request (400) :)

This appeared when I've tried to proxy_pass nginx incoming requests to another server. And then even getting rid of nginx, and making just a DNAT/SNAT with iptables, still gives same result. So it's about other server issue, it's definitely not nginx.

@davitv
Copy link

davitv commented Apr 16, 2015

I have same trouble right now,@holms. I think, you are write.

@seanpmaxwell
Copy link

i'm the same problem as well even with ALLOWED_HOSTS = ['*'] someone needs to post a solution soon

@tlevine
Copy link

tlevine commented May 7, 2015

Hmm I'm also having such an issue. Why four comments in the past month after no comments for a year? Maybe an update broke something?

After noticing the problem, I upgraded my system packages (apt-get dist-upgrade because it's Debian) and all of the relevant Python packages and still have the problem.

@jhagege
Copy link

jhagege commented Jul 8, 2015

+1

@shihweilo
Copy link

I too had this issue and solved it by adding following to wsgi.py

import sys, os
sys.path.insert(0, '/opt/python/current/app')   # I use ElasticBeanstalk, modify to your project path
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your.settings')

@attolee
Copy link

attolee commented Jun 24, 2016

I got 400 error, because I forget to import a model class in views.

@gautamyadavs
Copy link

+1

@brunnooliver7
Copy link

+1

@engmsilva
Copy link

+1

@la5chine
Copy link

+1

@mahes1287
Copy link

+1

@yalattas
Copy link

+1

@mlmr
Copy link

mlmr commented Feb 8, 2023

+1

@iamrahulrnair
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment