Django "You may need to add 'Server IP' to ALLOWED_HOSTS"

Hello i recongnize this question has been asked before but i havnt found a clear answer.

Some Information: Im using nginx to proxy. Gunicorn for WSGI. I do have a domain name linked to my ip. Everything works fine. All my request sent through the domain name not ip.

What i have tried: i tried to block requests with the IP address from nginx because initally i thought some bots are making requests using the IP address directly. But the request still got to django since this error is being thrown by django. so its either nginx didnt block them or something is converting the domain name to the Server IP address internally not sure.

Preferred Requirements: Im looking for a solution AND a reason. I understand many people suggest to just add the IP to the allowed hosts. which is obvious but is there a valid reason to do so as in what is causing this issue to begin with and why is the solution adding the ip address rather than remving whats making the server ip requests which seems like the correct approach. Obviously i dont know the cause so i dont know if thats possible at all.

Thank you in advanced <3

this is the error/warning im getting on the server

django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'Server IP'. You may need to add 'Server IP' to ALLOWED_HOSTS.
ERROR 09:50:52 PM exception Invalid HTTP_HOST header: 'Server IP'. You may need to add 'Server IP' to ALLOWED_HOSTS.
Traceback (most recent call last):
  File "/home/ubuntu/Project/Settings/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Project/Settings/venv/lib/python3.12/site-packages/django/utils/deprecation.py", line 128, in __call__
    response = self.process_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Project/Settings/venv/lib/python3.12/site-packages/django/middleware/common.py", line 48, in process_request
    host = request.get_host()
           ^^^^^^^^^^^^^^^^^^
  File "/home/ubuntu/Project/Settings/venv/lib/python3.12/site-packages/django/http/request.py", line 151, in get_host
    raise DisallowedHost(msg)
Вернуться на верх