简体   繁体   中英

ALLOWED_HOSTS error in Django app on Digital Ocean. Setting the variable does not resolve the error

I followed this guide to set up a Django app on DO:

https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

However, I'm unable to get past the common ALLOWED_HOSTS error, where everyone typically forgets to put anything in the list and its a simple fix. Even with the correct values entered, or with the catch-all "*", the error persists. I've never had this happen before, so I'll put my configuration files here to get some more eyes on it. Any ideas as to what could be causing this?

contents of /etc/uwsgi/sites/myapp.ini:

[uwsgi]
project = myapp
uid = myuser
base = /home/%(uid)

chdir = %(base)/%(project)
home = %(base)/%(project)/venv
module = %(project).wsgi:application

master = true
processes = 5

socket = /run/uwsgi/%(project).sock
chown-socket = %(uid):www-data
chmod-socket = 660
vacuum = true

contents of /etc/systemd/system/uwsgi.service:

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /run/uwsgi; chown myuser:www-data /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

contents of /etc/nginx/sites-available/myapp:

server {
    listen 80;
    server_name mydomain.com www.mydomain.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/myuser/myapp_static;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/run/uwsgi/myapp.sock;
    }
}

UPDATE

error message displaying in browser:

DisallowedHost at /
Invalid HTTP_HOST header: 'mydomain.com'. You may need to add 'mydomain.com' to ALLOWED_HOSTS.
Request Method: GET
Request URL:    mydomain.com
Django Version: 1.11
Exception Type: DisallowedHost
Exception Value:    
Invalid HTTP_HOST header: 'mydomain.com'. You may need to add 'mydomain.com' to ALLOWED_HOSTS.
Exception Location: /home/myuser/myapp/venv/lib/python3.5/site-packages/django/http/request.py in get_host, line 113
Python Executable:  /usr/local/bin/uwsgi
Python Version: 3.5.2
Python Path:    
['.',
 '',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/home/myuser/myapp/venv/lib/python3.5/site-packages']

Oh, well now this is interesting. According to the django error page, the current value of ALLOWED_HOSTS is my server's ip address (in a list of course). But its definitely set to ['*'] right now in the settings.py file. What could possibly be overriding that variable?

You should set ALLOWED_HOSTS appropriately in Django settings.py . like this.

ALLOWED_HOSTS = ['mydomain.com', 'www.mydomain.com']

The same thing is written in the article of Digital ocean. Let's see again well.

Whoa, ok, I found it. I was confused because the value of ALLOWED_HOSTS wasn't updating, and I kept restarting nginx. But the solution here was to RESTART UWSGI using:

sudo systemctl restart uwsgi

Haven't really had to do that with other Django stacks before, but now I know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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