Django and Elastic Beanstalk URL health checks
<p>I have a Django webapp. It runs inside Docker on Elastic Beanstalk.</p>

<p>I'd like to specify a <a href="https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.managing.elb.html#using-features.managing.elb.healthchecks" rel="noreferrer">health check URL</a> for slightly more advanced health checking than "can the ELB establish a TCP connection".</p>

<p>Entirely reasonably, the ELB does this by connecting to the instance over HTTP, using the instance's hostname (e.g. <code>ec2-127-0-0-1.compute-1.amazonaws.com</code>) as the <code>Host</code> header.</p>

<p>Django has <a href="https://docs.djangoproject.com/en/1.9/ref/settings/#allowed-hosts" rel="noreferrer"><code>ALLOWED_HOSTS</code></a> which validates the <code>Host</code> header of incoming requests. I set this to my application's external domain via environment variable.</p>

<p>Unsurprisingly and entirely reasonably, Django thus rejects ELB URL health checks due to lack of matching <code>Host</code>.</p>

<p>We don't want to disable <code>ALLOWED_HOSTS</code> because we'd like to be able to  trust <a href="https://docs.djangoproject.com/en/1.9/ref/request-response/#django.http.HttpRequest.get_host" rel="noreferrer"><code>get_host()</code></a>.</p>

<p>The solutions so far seem to be:</p>

<ul>
<li>Somehow persuade Django to not care about <code>ALLOWED_HOSTS</code> for certain specific paths (i.e. the health check URL)</li>
<li>Do something funky like calling the EC2 info API on startup to get the host's FQDN and append it to <code>ALLOWED_HOSTS</code></li>
</ul>

<p>Neither of these seem particularly pleasant. Can anyone recommend a better / existing solution?</p>

<p>(For the avoidance of doubt, I believe this problem to be identical to the scenario of "Disabled <code>ALLOWED_HOSTS</code>, fronting HTTPD that filters on host" - I want the health check to hit <em>Django</em>, not a fronting HTTPD)</p>