【问题标题】:Django/Nginx DisallowedHost header errorDjango/Nginx DisallowedHost 标头错误
【发布时间】:2017-09-03 21:41:39
【问题描述】:

有几个这样的线程,但没有一个能回答我的问题。我有一个托管网站的服务器,如果您通过 ip 地址直接到那里,该网站运行良好,但是我买了一个 dns,我正在尝试将 dns 重定向到 ip。我正在使用 django 1.11.4(当前 pip 安装版本)。设置完所有 DNS 服务器后,我可以 ping 服务器,但如果我转到浏览器并输入我的 dns,我会收到以下错误:

Environment:


Request Method: GET

Request URL: http://thefelpub.com/

Django Version: 1.11.4
Python Version: 2.7.12
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'pub')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
  244.             response = middleware_method(request)

File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
  57.         host = request.get_host()

File "/usr/local/lib/python2.7/dist-packages/django/http/request.py" in get_host
  113.             raise DisallowedHost(msg)

Exception Type: DisallowedHost at /
Exception Value: Invalid HTTP_HOST header: 'thefelpub.com'. You may need to add u'thefelpub.com' to ALLOWED_HOSTS.

我已将我的 dns 添加到 ALLOWED_HOSTS=[] 列表中,但无论我尝试了多少个 dns 变体(包括“*”),每次尝试到达那里时都会遇到相同的错误。

【问题讨论】:

  • 添加ALLOWED_HOSTS=['*'] 后是否重启了django?
  • 是的,我每次尝试更改 ALLOWED_HOSTS 后都会重新启动 nginx
  • 您重启 gunicorn 而不是 nginx 以使更改生效

标签: python django nginx dns


【解决方案1】:

错误信息中已经提到,你肯定需要在ALLOWED_HOSTS中添加“thefelpub.com”。

ALLOWED_HOSTS = ['thefelpub.com']

或者,不推荐:

ALLOWED_HOSTS = [*]

【讨论】:

  • 此外,您需要在 nginx 可用站点中提供相同的域名才能将给定的答案添加到您的 seettings.py
  • 我试过了:ALLOWED_HOSTS = ['thefelpub.com'] 我也试过了:ALLOWED_HOSTS = ['*'] 都没有。稍后在代码中有: ALLOWED_HOSTS = ip_addresses() 我不太确定这是否会导致任何问题(它是由 Django 放在那里的)
  • @deaspo 我现在只做了大约 24 小时,所以我还是个菜鸟。如何在 nginx 中服务域名?
  • 您需要更多解释吗?我建议您投票并accept我的回答。
【解决方案2】:

将以下内容添加到你的ALLOWED_HOSTs = ['45.55.54.110', 'www.thefelpub.com', 'thefelpub.com'],保存settings.py文件然后重启gunicorn,service gunicorn restart重启Django。

【讨论】:

  • 我还没有加密它,因为我无法让 DNS 指向它。这是下一个任务,这让我望而却步。我添加了server_name example.com www.example.com; 并将示例更改为我的服务器名称,但现在如果我输入 thefelpub.com,我会看到 nginx 欢迎页面,而 www.thefelpub.com 给了我之前遇到的相同错误。 IP 仍在正常工作。
  • 所以问题是指向服务器的域名?你是如何配置dns设置的?您使用的是哪个托管公司?
  • 我正在使用 namecheap 指向数字云。 Namecheap 的文档记录很差,但是从数字云中我被告知将名称服务器设置为自定义并将它们指向数字云的 ns1.digitalocean.com、ns2.digitalocean.com 和 n3.digitalocean.com。 Digital Cloud 具有将 thefelpub.com 指向 ip 的“A”、将昵称指向 thefelpub.com 的 CNAME,以及在创建记录时预先构建的三个 NS。
  • 您在 settings.py 中添加了 ALLOWED_HOSTS = ['ipaddress','thefelpub.com','www.thefelpub.com'] 并重新启动了 gunicorn?您需要重新启动 gunicorn 才能对 .py 文件进行任何更改
  • 是的。 ALLOWED_HOSTS = ['thefelpub.com', 'www.thefelpub.com'] 现在在那里,我有 ALLOWED_HOSTS = ['thefelpub.com', 'www.thefelpub.com'] 并重新启动了 nginx。 service nginx stop 然后service nginx start 几次。 service nginx restart 也是。
猜你喜欢
  • 2017-03-26
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2014-06-28
  • 2018-04-05
  • 2018-04-26
  • 1970-01-01
相关资源
最近更新 更多