How to fix Invalid HTTP_HOST header error

When I deploy my django project to elasticbeanstalk I get the message in the log: “Invalid HTTP_HOST header: ‘172.12.2.90’. You may need to add ‘172.12.2.90’’ to ALLOWED_HOSTS.” and so on… I have to add 3 ip adresses like the one above in total for the health to be ok and no error/warning messages. Why is this the case? Where are these domains/IP addresses coming from? Is there any more convenient way to fix this so that I don’t have to add random IP adresses to the array ALLOWED_HOSTS = [“mydomain.com”] in settings.py?

I created a file and directories in .ebextensions/nginx/conf.d/myconf.conf. This was because according to Configuring the reverse proxy - AWS Elastic Beanstalk, it says “To extend Elastic Beanstalk’s default nginx configuration, add .conf configuration files to a folder named .ebextensions/nginx/conf.d/”.

myconf.conf:

if ( $host !~* ^("mydomain.com"|"www.mydomain.com")$ ) {
return 444;
}

This didn’t work it still gives the same message as before.

The way I interpret it is that my django project is on an AWS physical server somewhere and nginx is a middleman between that and my browser. My understanding of Django and AWS is quite basic so I would appreciate it if the answer was as simple as possible.

Thanks in advance!

EDIT: I removed .ebextensions/nginx/conf.d/ . In a file located at ./.platform/nginx/conf.d/system_config.conf, where . is the project root directory, meaning in the same directory as manage.py:

server {

server_name mydomain.com;

location / {
    proxy_pass mydomain.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

}

proxy_set_header Host $host;: This directive sets the value of the “Host” header forwarded to the backend server. It ensures that the backend server knows the original hostname the user requested.

proxy_set_header X-Real-IP $remote_addr;: This directive sets the value of the “X-Real-IP” header forwarded to the backend server. It is used to send the actual IP address of the client (user) making the request to the backend server.

Maybe I am mistaken but shouldn’t my code above work? It doesn’t, it still gives me the same error “You may need to add ‘172.12.2.90’’ to ALLOWED_HOSTS.”

I then tried this, which I thought should work since “elasticbeanstalk” is in the name of of my allowed host but not in “172.12.2.90”:

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.:]*elasticbeanstalk[a-zA-z0-9.:]*")

That didn’t work either…