By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How to fix Invalid HTTP_HOST header error when deploying django project with elasticbeanstalk?

0

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."

asked 9 months ago767 views
1 Answer
0

Hello.

Wouldn't it be resolved if you set "ALLOWED_HOSTS" to "*" as shown below?

ALLOWED_HOSTS = ["*"]
profile picture
EXPERT
answered 9 months ago
profile picture
EXPERT
reviewed 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions