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 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-nginx.html, 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!
EDIT1: 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."
The issue beacuse of HOST HEADER. Because the upstream application is using Load balancing the request may come from any IP of load balancer.
I think the better solution is to add a custom HOST HEADER manually in request while forwarding the request to django. You can do something like this.
in your nginx.conf add the custom host like this.
And then just the custom domain "mydomain.com" in ALLOWED_HOSTS