title: gunicorn + nginx: Server via socket or proxy?
body: <p>I've seen two strategies for hosting a django application with gunicorn and nginx. </p>
  
  <p>One strategy is to run gunicorn on a network port. For example (from <a href="http://goodcode.io/blog/django-nginx-gunicorn/" rel="noreferrer">http://goodcode.io/blog/django-nginx-gunicorn/</a>):</p>
  
  <pre><code>location / {
      proxy_pass_header Server;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Scheme $scheme;
      proxy_connect_timeout 10;
      proxy_read_timeout 10;
      proxy_pass http://localhost:8000/;
  }
  </code></pre>
  
  <p>Another strategy is to bind gunicorn to a UNIX socket on startup (e.g. <a href="http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/" rel="noreferrer">http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/</a>)</p>
  
  <pre><code>upstream hello_app_server {
      server unix:/tmp/gunicorn.sock fail_timeout=0;
  }
  
  ...
  
  location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (!-f $request_filename) {
          proxy_pass http://hello_app_server;
          break;
      }
  }
  </code></pre>
  
  <p>Thoughts on which strategy is superior? Any any comments on the proper way to do each? I am leaning towards the socket approach because of the overhead I imagine is introduced by TCP. I am most concerned about the differences about headers, connect timeouts and such between implementation examples I have seen.</p>