Search code examples
djangoubuntunginxamazon-ec2

Invalid HTTP_HOST on Django (EC2 Aws Ubuntu)


I'm having trouble deploying my Django application. I'm running it using nohup on port 8000 and using nginx with SSL certificate configuration, masking the domain to port 80.

Here are my default Nginx settings:

server {
    listen 80;
    listen 443 ssl;
    server_name fmartns.dev;

    ssl_certificate /etc/letsencrypt/live/fmartns.dev/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/fmartns.dev/privkey.pem;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/fmartns.dev;
    }

    location / {
        include proxy_params;
        proxy_pass http://3.82.145.23:8000;  # Port where Django is running
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Django's settings.py configuration:

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['fmartns.dev', '*']

SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CSRF_TRUSTED_ORIGINS = ['http://fmartns.dev']

Error log:

DisallowedHost at /
Invalid HTTP_HOST header: 'fmartns.dev,fmartns.dev'. The domain name provided is not valid according to RFC 1034/1035.

Any ideas on how I can resolve this? I've tried various tutorials and configurations both in my settings.py and in the default nginx settings.


Solution

  • You are including the headers twice with the include proxy_params and the proxy_set_header.

    Since you are already including (include proxy_params) you can safely remove the three proxy_set_header lines in your nginx configuration.