Nginx – Django App Public IP Works but `400 Bad Request Error` when A Record Update

a-recordamazon ec2amazon-web-servicesdjangonginx

I've got a Django application deployed on an Ubuntu 16.04 EC2 instance using nginx and gunicorn.

Unfortunately, after a successful deployment with my public IP, updating my A record with the public IP yielded a 404 Bad Request error at my domain–even after waiting a long while to make sure DNS changes propagated. Accessing the application by the public IP continues to work. I did contact my name server host, to confirm if I correctly set my A record settings: they said the DNS change appeared successful, but could not help as it appeared it was an issue on AWS's side.

Is there a specific reason why utilizing my public IP in my A record is not working correctly and is not pointing to my application?

Thank you for anyone who might be able to help me get my domain to successfully point to my deployed django project. At present, the accessing the application via the public IP still works fine, the A record appears to be updated, but any attempts to load the domain (not the direct IP), renders a 400 Bad Request error.

Best Answer

I figured out a resolution. It does appear that using the public IP is acceptable, but I failed to add my domain name both to the ALLOWED_HOSTS file and to my nginx configuration.

Doing the following, I was able to get my domain to successfully point to my AWS EC2 deployment:

I did three things:

  • First, I updated my ALLOWED_HOSTS:
    • ALLOWED_HOSTS = ['12.345.67.890', 'sub.domain.com', 'www.sub.domain.com']
  • Then, I also edited the server_name setting in my nginx configuration:
    • server_name 12.345.67.890 sub.domain.com www.sub.domain.com;
  • Lastly, I restarted nginx and rebooted the machine to make sure it all worked:
    • sudo service nginx restart
    • sudo reboot

After this, my page loaded successfully.

Related Topic