For no apparent reason that I can think of, I am getting the:
Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.
The IP address been correctly added to my settings.py
file. I am using Ubuntu 22.04.4 LTS (GNU/Linux 6.5.0-1014-aws x86_64)
with the latest version of Django, which is Django 5.0.4
As per SO's requirements, these are the fully reproduceable steps I took which brought me to this issue:
I created an Amazon EC2 Instance (free tier).
The Outbound NSG settings are as follows:
Port Range: 8000 Protocol: TCP Source: 0.0.0.0/0
Port Range: 80 Protocol: TCP Source: 0.0.0.0/0
Port Range: 443 Protocol: TCP Source: 0.0.0.0/0
The Inbound NSG Settings are as follows:
Port Range: 443 Protocol: TCP Source: 0.0.0.0/0
I connected to my Ubuntu EC2 Server and successfully cloned my GitHub repository via the
ssh
methodology.I ran the following commands:
sudo apt updatesudo apt upgrade
sudo apt install git
sudo apt install python3 python3-pip
sudo apt install python3-venv
I then continued to ensure that pip was installed to the latest version, before installing my
venv
and dependencies, by running the following commands:pip install --upgrade pip
cd .ssh/<repository-name>
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt ## python==3.10.12## whitenoise==6.6.0## pillow==10.3.0## requests==2.21.10## django_bootstrap_icon
(Note: it did not acknowledge installing Request however I ran the following command in my
venv
:The following message ensued, meaning this package has already been installed:
(venv) ubuntu@ip-172-31-39-0:~/.ssh/<REPO-NAME>$ pip install requestsRequirement already satisfied: requests in ./venv/lib/python3.10/site-packages (2.31.0)Requirement already satisfied: charset-normalizer<4,>=2 in ./venv/lib/python3.10/site-packages (from requests) (3.3.2)Requirement already satisfied: idna<4,>=2.5 in ./venv/lib/python3.10/site-packages (from requests) (3.7)Requirement already satisfied: urllib3<3,>=1.21.1 in ./venv/lib/python3.10/site-packages (from requests) (2.2.1)Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.10/site-packages (from requests) (2024.2.2)
I then ran the following command to run the Django migrations:
python3 manage.py migrate
(Running the ``collectstatic
\
command was already done in the local server.)I then ran started the server by running the following command:
python3 manage.py runserver 0.0.0.0:8000
I then went to my browser going to
http://13.60.32.132:8000
My
settings.py
is as follows:ALLOWED_HOSTS = ['13.60.32.132', '127.0.0.1']
The Django debug page traceback is as follows, under
Request Information
>Settings
:ALLOWED_HOSTS ['13.53.67.74', '127.0.0.1']
**As you can see, Django still think's I'm trying to access an IP address
13.53.67.74
and not13.60.32.132
. My the correct IP address (13.60.32.132
) is set in mysettings.py
file. Why does Django believe that I am attempting to access an IP address which no longer exists?Completely baffled.**
I have doubled checked the NSG settings, I expected to see my website when accessing the AWS-assigned IP address by visiting http://13.60.32.132:8000
Furthermore, I have rebooted the server and the unwanted result is the same.
I have tried setting my ALLOWED_HOSTS
to [*]
- which is not advisable in production - however, this isn't really the issue. The issue is that Django thinking I'm trying to access a non-existent IP address.
The traceback is as follows:
Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Traceback (most recent call last): File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__ response = self.process_request(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request host = request.get_host() File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/http/request.py", line 151, in get_host raise DisallowedHost(msg)django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.ERROR:django.security.DisallowedHost:Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Traceback (most recent call last): File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__ response = self.process_request(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request host = request.get_host() File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/http/request.py", line 151, in get_host raise DisallowedHost(msg)django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Bad Request: /WARNING:django.request:Bad Request: /[15/Apr/2024 11:04:30] "GET / HTTP/1.1" 400 68141Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Traceback (most recent call last): File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__ response = self.process_request(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request host = request.get_host() File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/http/request.py", line 151, in get_host raise DisallowedHost(msg)django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.ERROR:django.security.DisallowedHost:Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Traceback (most recent call last): File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/utils/deprecation.py", line 133, in __call__ response = self.process_request(request) File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/middleware/common.py", line 48, in process_request host = request.get_host() File "/home/ubuntu/.ssh/<xxxxxxxxx>/venv/lib/python3.10/site-packages/django/http/request.py", line 151, in get_host raise DisallowedHost(msg)django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '13.60.32.132:8000'. You may need to add '13.60.32.132' to ALLOWED_HOSTS.Bad Request: /favicon.icoWARNING:django.request:Bad Request: /favicon.ico[15/Apr/2024 11:04:31] "GET /favicon.ico HTTP/1.1" 400 68105