NGINX 重定向导致“DisallowedHost at /” Django

NGINX 重定向导致“DisallowedHost at /” Django

我已经在 Ubuntu 18.04.1 LTS 上安装了 nginx,并在 digital ocean 上安装了 Django 一键应用程序,我有这个配置

设置.py

# Find out what the IP addresses are at run time
# This is necessary because otherwise Gunicorn will reject the connections
def ip_addresses():
    ip_list = []
    for interface in netifaces.interfaces():
        addrs = netifaces.ifaddresses(interface)
        for x in (netifaces.AF_INET, netifaces.AF_INET6):
            if x in addrs:
                ip_list.append(addrs[x][0]['addr' ])
    return ip_list


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

当我跑步的时候 企业在线

DisallowedHost at /
Invalid HTTP_HOST header: 'enterprise.online'. You may need to add enterprise.online' to ALLOWED_HOSTS.

我尝试修改此行设置.py

ip_list.append(addrs[x][0][u'enterpise.online' ])

nginx发送消息:

502 Bad Gateway

nginx 设置

/etc/nginx/sites-enabled/...

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/django_project/django_project/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/django_project/django_project/static;
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://app_server;
    }

}

我不清楚,激活 www.enterprise.online 的下一步是什么。当我修改设置时,出现同样的错误。另一方面,我的 DNS 配置正常

答案1

这是你的 django 设置的问题。此错误意味着用于连接的 IP 没有显示在settings.ALLOWED_HOSTS列表. 确保你的settings.py文件中有如下一行:


ALLOWED_HOSTS = ip_addresses()

相关内容