Cliff Helsel
Cliff Helsel

Reputation: 940

Django Invalid HTTP_HOST header Elastic Beanstalk

I'm running a large hobby site on AWS ELB. I've tried to resolve this problem searching StackOverflow but nothing quite works. I updated my settings.py and ALLOWED_HOSTS as follows:

ALLOWED_HOSTS = ['127.0.0.1','.amazonaws.com','mysite.com','.elasticbeanstalk.com','localhost',]

import requests
EC2_PRIVATE_IP = None
try:
  EC2_PRIVATE_IP = requests.get("http://169.254.169.254/latest/meta-data/local-ipv4", timeout = 0.50).text
except requests.exceptions.RequestException:
  pass

if EC2_PRIVATE_IP:
  ALLOWED_HOSTS.append(EC2_PRIVATE_IP)

The ELB Health Check DOES work.

However, I'm still getting thousands of error emails daily. For example:

Invalid HTTP_HOST header: '54.84.163.167'. You may need to add '54.84.163.167' to ALLOWED_HOSTS.

Report at / Invalid HTTP_HOST header: '54.84.163.167'. You may need to add '54.84.163.167' to ALLOWED_HOSTS.

My site architecture is Cloudfront, to ELB, to Django. From what I can tell the IP address shown (and it does change regularly) is part of Amazons network. I suspect it is the load balancer itself but I don't know a way to confirm that and identify the load balancer ip address to add to allowed hosts.

Any other thoughts or ideas?

Upvotes: 7

Views: 1912

Answers (1)

James Dean
James Dean

Reputation: 4451

This appears to be a AWS IP Address, probably from EC2 or ELB. dig -x 54.84.163.167 +short ec2-54-84-163-167.compute-1.amazonaws.com.

This isn't cloudfront regional edge ip because it is not listed here: http://d7uri8nf7uskq.cloudfront.net/tools/list-cloudfront-ips (CLOUDFRONT_REGIONAL_EDGE_IP_LIST)

Additionally health check from ELB to Ec2 occurs from ELB ENI Private IP address, I'm not sure which AWS resource owns this but it seems to be making request directly on ELB bypassing CloudFront.

Upvotes: 0

Related Questions