Deploying django app with docker, ALLOWED_HOSTS
Asked Answered
U

2

6

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.

Unappealable answered 18/12, 2015 at 17:27 Comment(0)
U
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.

Unappealable answered 25/12, 2015 at 14:30 Comment(1)
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.Hilarity
H
3

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()
Horehound answered 24/6, 2022 at 10:57 Comment(3)
What is the reason for this ?Szombathely
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]Horehound
Sure, but why don't you skip the convertion to a list by entering a list in the .env file ?Szombathely
U
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.

Unappealable answered 25/12, 2015 at 14:30 Comment(1)
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.Hilarity

© 2022 - 2024 — McMap. All rights reserved.