title: Why is nginx accepting requests with a Host header that doesn't match server_name?
body: <p>I have a site set up this way: nginx as a proxy server, proxying requests tu a gunicorn instance serving a Django site through a UNIX socket.</p>
  
  <p>This is my nginx configuration:</p>
  
  <pre><code>server {
      listen 80;
      server_name api.mysite.com;
  
      location /static/ {
          alias /webapps/mysite/static/;
          autoindex off;
      }
  
      location / {
          include proxy_params;
          proxy_pass http://unix:/webapps/mysite/mysite.sock;
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Is my understanding that nginx, when receiving a request, matches the <code>Host</code> header against the server_name parameter of the server block and, if it matches, it serves it. However, nginx seems to be trying to serve (passing the request to my Django server) request with a <code>Host</code> header different than api.mysite.com. Django has a setting named <code>ALLOWED_HOSTS</code>  (in my case set to <code>['api.mysite.com']</code>) that performs further checking of the <code>Host</code> header and raises an error if the request <code>Host</code> header doesn't match, which shouldn't happen because nginx is supposedly already filtering this. The thing is that I'm seeing errors raised by Django which look like this:</p>
  
  <ul>
  <li>Invalid HTTP_HOST header: <strong>'/webapps/mysite/mysite.sock:'</strong>. The domain name provided is not valid according to RFC 1034/1035.</li>
  <li>Invalid HTTP_HOST header: <strong>'testp1.piwo.pila.pl'</strong>. You may need to add 'testp1.piwo.pila.pl' to ALLOWED_HOSTS.</li>
  <li>Invalid HTTP_HOST header: <strong>'xxx.xxx.xxx.xxx'</strong> <em>(The real IP of my server)</em>. You may need to add 'xxx.xxx.xxx.xxx' to ALLOWED_HOSTS.</li>
  </ul>
  
  <p>A couple of things:</p>
  
  <ul>
  <li>I know that the requests are coming through nginx because they show up in nginx logs and it would be impossible anyway to hit directly the gunicorn server because I'm proxying through a UNIX socket and not through HTTP.</li>
  <li>I know that these requests are coming from a bot looking for free proxies to use, but I don't really care much about that. What I'm really intrigued by is the request with the <code>Host</code> header set to the path on the local filesystem of my gunicorn/nignx UNIX socket.</li>
  </ul>
  
  <p>Any clue?</p>