简体   繁体   English

使用 docker 部署 django 应用程序,ALLOWED_HOSTS

[英]Deploying django app with docker, ALLOWED_HOSTS

I am trying to run a django app with docker, I am using the cookiecutter-django template that exists.我正在尝试使用 docker 运行 django 应用程序,我正在使用存在的cookiecutter-django 模板 When trying to run the app however, I get the following error:但是,当尝试运行该应用程序时,我收到以下错误:

Bad Request (400)错误请求 (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.这很奇怪,因为我已经将 192.168.99.100 添加到 ALLOWED_HOSTS(也尝试过 ["*"]),所以我不太明白这个错误来自哪里。

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.添加不带括号的* (或实际主机)有效。

For those who defined allowed hosts in .env as an environment variable, you should use the following format (without brackets):对于那些将.envallowed hosts定义为环境变量的人,您应该使用以下格式(不带括号):

in .env file:在 .env 文件中:

DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 my_domain.com

and in settings.py并在 settings.py

ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "127.0.0.1").split()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM