Kathiravan Natarajan
Kathiravan Natarajan

Reputation: 3488

How to fix Django error: DisallowedHost at / Invalid HTTP_HOST header: ... You may need to add ... to ALLOWED_HOSTS

I am trying to develop a website using Django framework and launched using DigitalOcean.com and deployed the necessary files into django-project.

I had to include static files into Django-project and After collecting static files, I tried to refresh my ip

I am including the tutorials which I have used to create the website. https://www.pythonprogramming.net/django-web-server-publish-tutorial/

I am getting the following error :

DisallowedHost at / Invalid HTTP_HOST header: '198.211.99.20'. You may need to add u'198.211.99.20' to ALLOWED_HOSTS.

Can somebody help me to fix this ? This is my first website using Django framework.

Upvotes: 191

Views: 260961

Answers (6)

George Poliovei
George Poliovei

Reputation: 1129

settings.py

ALLOWED_HOSTS = ['*'] # if you are in dev or docker

Don't do this in production if you are not using docker, just put the IP address.

Upvotes: 12

Abdullah Khawer
Abdullah Khawer

Reputation: 5678

I was testing my Django app on minikube and because of the probes, it was failing with the following error:

Invalid HTTP_HOST header: '10.244.0.8:8080'. You may need to add '10.244.0.8' to ALLOWED_HOSTS.
Bad Request: /

I added the httpHeaders in my probes and it worked after that.

Before:

livenessProbe:
  httpGet:
    path: / # Replace with an endpoint that returns a 200 status code if the app is healthy
    port: 8080
  initialDelaySeconds: 10 # Delay before the first probe is executed
  periodSeconds: 10 # How often to perform the probe
readinessProbe:
  httpGet:
    path: / # Replace with an endpoint that indicates readiness
    port: 8080
  initialDelaySeconds: 5 # Delay before the first probe is executed
  periodSeconds: 5 # How often to perform the probe

After:

livenessProbe:
  httpGet:
    path: / # Replace with an endpoint that returns a 200 status code if the app is healthy
    port: 8080
    httpHeaders:
    - name: Host
      value: localhost
  initialDelaySeconds: 10 # Delay before the first probe is executed
  periodSeconds: 10 # How often to perform the probe
readinessProbe:
  httpGet:
    path: / # Replace with an endpoint that indicates readiness
    port: 8080
    httpHeaders:
    - name: Host
      value: localhost
  initialDelaySeconds: 5 # Delay before the first probe is executed
  periodSeconds: 5 # How often to perform the probe

Upvotes: 0

Paul Munyao
Paul Munyao

Reputation: 195

You can add ALLOWED_HOSTS to your settings file or env file:

ALLOWED_HOSTS = [".localhost", "127.0.0.1", "[::1]"]

Upvotes: 1

if no other answer work you can try modifying manage.py and add this three lines

from django.utils.regex_helper import _lazy_re_compile
import django.http.request
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

to end up having something like this:

import os
import sys

from django.utils.regex_helper import _lazy_re_compile
import django.http.request    
django.http.request.host_validation_re = _lazy_re_compile(r"[a-zA-z0-9.:]*")

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project01.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

as it is explained in this post: How to Solve "The domain name provided is not valid according to RFC 1034/1035" in Django during Development

Upvotes: 0

Darwin
Darwin

Reputation: 2037

In your project settings.py file,set ALLOWED_HOSTS like this :

ALLOWED_HOSTS = ['62.63.141.41', 'namjoosadr.com']

and then restart your apache. in ubuntu:

/etc/init.d/apache2 restart

Upvotes: 2

Prakhar Trivedi
Prakhar Trivedi

Reputation: 8526

The error log is straightforward. As it suggested,You need to add 198.211.99.20 to your ALLOWED_HOSTS setting.

In your project settings.py file,set ALLOWED_HOSTS like this :

ALLOWED_HOSTS = ['198.211.99.20', 'localhost', '127.0.0.1']

For further reading read from here.

Upvotes: 300

Related Questions