5

I am trying to run a django app with docker, I am using the cookiecutter-django template that exists. When trying to run the app however, I get the following error:

Bad Request (400)

When looking at the log from the terminal I see the following:

django_1 | [2015-12-18 17:08:04 +0000] [15] [INFO] Booting worker with pid: 15
django_1 | [2015-12-18 17:08:04 +0000] [16] [INFO] Booting worker with pid: 16
django_1 | [2015-12-18 17:08:04 +0000] [18] [INFO] Booting worker with pid: 18
django_1 | [2015-12-18 17:08:04 +0000] [20] [INFO] Booting worker with pid: 20
django_1 | ERROR 2015-12-18 18:08:07,072 base 18 140496642320128 Invalid HTTP_HOST header: '192.168.99.100'. You may need to add '192.168.99.100' to ALLOWED_HOSTS.
nginx_1 | 192.168.99.1 - - [18/Dec/2015:17:08:08 +0000] "GET /admin HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"
django_1 | ERROR 2015-12-18 18:08:08,570 base 20 140496642320128 Invalid HTTP_HOST header: '192.168.99.100'. You may need to add '192.168.99.100' to ALLOWED_HOSTS.
nginx_1 | 192.168.99.1 - - [18/Dec/2015:17:08:09 +0000] "GET /favicon.ico HTTP/1.1" 400 37 "http://192.168.99.100/admin" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"
django_1 | ERROR 2015-12-18 18:08:09,842 base 18 140496642320128 Invalid HTTP_HOST header: '192.168.99.100'. You may need to add '192.168.99.100' to ALLOWED_HOSTS.
nginx_1 | 192.168.99.1 - - [18/Dec/2015:17:08:11 +0000] "GET /favicon.ico HTTP/1.1" 400 37 "http://192.168.99.100/admin" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36" "-"

This is strange because I already added 192.168.99.100 to the ALLOWED_HOSTS (also tried ["*"]), so I don't really understand where this error comes from.

pydanny
  • 7,954
  • 6
  • 34
  • 42
hY8vVpf3tyR57Xib
  • 3,574
  • 8
  • 41
  • 86

2 Answers2

2

So in the end the brackets generated the error because the hosts were set in my environment variables. Adding * (or the actual host) without brackets worked.

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
hY8vVpf3tyR57Xib
  • 3,574
  • 8
  • 41
  • 86
  • 3
    Careful with the wildcard, makes ALLOW_HOSTS useless. "*" can be used behind an Nginx proxy server if you take care of unwanted domains from the Nginx configuration. – Marcs Mar 02 '16 at 10:31
1

For those who defined allowed hosts in .env as an environment variable, you should use the following format (without brackets):

in .env file:

DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 my_domain.com

and in settings.py

ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "127.0.0.1").split()
Masoud Gheisari
  • 949
  • 1
  • 9
  • 21
  • What is the reason for this ? – Florent Aug 31 '22 at 16:27
  • Because `localhost 127.0.0.1 my_domain.com` is interpreted as a string. We convert this string to a list of allowed hosts by `.split()` method. The result is : `[localhost, 127.0.0.1, my_domain.com]` – Masoud Gheisari Sep 01 '22 at 09:39
  • Sure, but why don't you skip the convertion to a list by entering a list in the `.env` file ? – Florent Sep 01 '22 at 13:44