After adding my domain to ALLOWED_HOSTS in django settings I am still getting a 502 error. Not sure why my settings are not recognized.

Posted October 12, 2017 23.7k views
NginxDjangoConfiguration ManagementUbuntu 16.04

So, I created an Ubuntu droplet with nginx, gunicorn and django. I added my domain and when trying to access my django site I got a 502 error. I read in several places that adding my domain to ALLOWEDHOSTS should remedy the problem so I did that and still got the error. To make a long story that should have been shorter shorter I finally started gunicorn in the console and the stacktrace I am getting indicates that my domain probably needs to be added to ALLOWEDHOSTS but I already added it. After digging a little further I notice that there are three files that contain an ALLOWEDHOSTS variable. There is ./var/lib/digitalocean/, ./var/lib/digitalocean/ and /home/django/djangoproject/djangoproject/ So far I have attempted adding my domain into each of thes files and still recieve the same error. This is a stupid, really really frustrating error. Anyone have any suggestions or have the same issue?

1 comment
  • Nevermind, I had debug set to true in settings. I changed that now I am getting a 400 error I believe from nginx and it doesn’t look like like I am not getting to gunicorn at all at this point.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

I figured out what the problem was. I didn’t realize that at the bottom of my file ALLOWED_HOSTS was being overwritten when the IP addresses are looked up at runtime. Sooo, ALLOWED_HOSTS appears near the beginning of the file which contains the list of your supplied ip addresses and/or domain names and another one at the very end of the file which is set as follows:

Find out what the IP addresses are at run time

This is necessary because otherwise Gunicorn will reject the connections

def ipaddresses():
list = []
for interface in netifaces.interfaces():
addrs = netifaces.ifaddresses(interface)
for x in (netifaces.AF_INET, netifaces.AFINET6):
if x in addrs:
return ip_list

Discover our IP address

ALLOWED_HOSTS = ip_addresses()

I had to change ALLOWED_HOSTS = ip_addresses() to ALLOWED_HOSTS += ip_addresses() so that my entries near the beginning of the file weren’t overwritten.

Doesn’t work for me for some reason.

Hi, there, I am having the same problem I believe. But I am very new here. Would you please elaborate on your solution, please? Thank you!

So, django has a file that it uses for application-wide configuration. In the settings file generated for my app I was setting the ALLOWED_HOSTS variable but then at the end of the file it was being overwritten. The following line at the end of the settings file:

ALLOWEDHOSTS += ipaddresses()

“appends” the output from the ipaddresses call to what I set ALLOWEDHOSTS to previously in the file.