Django DisallowedHost: “Invalid HTTP_HOST header” despite correct ALLOWED_HOSTS and nginx configuration
I am running a Django 4.2.20 application on an Ubuntu server with Gunicorn and nginx as reverse proxy. However, I get the following error when I access the website via the public IP:
Exception Type: DisallowedHost at /
Exception Value: Invalid HTTP_HOST header: '89.168.121.242,89.168.121.242'. The domain name provided is not valid according to RFC 1034/1035.
I have already checked the following: ALLOWED_HOSTS in settings.py:
ALLOWED_HOSTS = ['ipaddress', 'localhost']
nginx configuration (/etc/nginx/sites-available/familyapp):
server {
listen 80;
server_name ipaddress;
location /static/ {
alias /home/familyapp/FamilyApp/familyapp/static/;
}
location /media/ {
alias /home/familyapp/FamilyApp/media/;
}
location / {
include proxy_params;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://unix:/home/ubuntu/sockets/gunicorn.sock;
}
}
nginx syntax check & restart
sudo nginx -t # No errors
sudo systemctl restart nginx
sudo systemctl restart gunicorn
Gunicorn started manually
gunicorn --bind unix:/home/ubuntu/s
There's an extra dot (.) at the end of '89.168.121.222.', which makes it invalid, remove the dot.
ALLOWED_HOSTS = ['89.168.121.222', 'localhost']
Fix the nginx server_name and proxy_set_header.
server {
listen 80;
server_name 89.168.121.222;
location /static/ {
alias /home/familyapp/FamilyApp/familyapp/static/;
}
location /media/ {
alias /home/familyapp/FamilyApp/media/;
}
location / {
include proxy_params;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://unix:/home/ubuntu/sockets/gunicorn.sock;
}
}