title: Mysterious ~1min delay in HTTP POST between browser and Nginx
body: <p>We've lately been experiencing a very strange but very consistent delay when POSTing from client-side javascript to our server.</p>
  
  <p><strong>Here's our technology stack, from front to back:</strong></p>
  
  <ol>
  <li>Custom javascript client code</li>
  <li>Backbone.js</li>
  <li><a href="https://gist.github.com/3789008">Custom Backbone.sync() implementation</a></li>
  <li>jQuery.ajax() (1.7.2)</li>
  <li>XmlHttpRequest</li>
  <li>Browser (verified on both Firefox and Chrome)</li>
  <li>Internet</li>
  <li>Nginx front-end</li>
  <li>Intranet (via Nginx <code>http://</code> upstream)</li>
  <li>Nginx back-end</li>
  <li>Gunicorn (via Nginx <code>unix://</code> upstream socket)</li>
  <li>Django 1.4</li>
  <li>django-tastypie</li>
  </ol>
  
  <p>(Side note: do you ever just step back in awe at how <em>complicated</em> web development is?)</p>
  
  <p><strong>Here's a timeline of events:</strong></p>
  
  <ol>
  <li>Client code calls <code>.save()</code> on a newly created Backbone <a href="https://gist.github.com/3789008#L358"><code>APIModel</code></a>.</li>
  <li>Our custom <a href="https://gist.github.com/3789008#L245"><code>.sync()</code></a> winds its way to <a href="https://gist.github.com/3789008#L180"><code>client.send()</code></a> which dispatches the newly created object to <a href="http://api.jquery.com/jQuery.ajax/"><code>$.ajax()</code></a>.</li>
  <li>The resulting XmlHttpRequest POSTs. The request appears in the browser dev tool's Network pane, marked as <em>pending</em>.</li>
  <li>The HTTP request reaches Nginx, which routes it to the django-tastypie backend.</li>
  <li>Tastypie handles the request promptly and flawlessly, creating the resource, and returning a 201 CREATED response, with a <code>Location</code> header pointing to the new resource.</li>
  <li>Nginx logs the request and (ostensibly) sends the response.</li>
  <li><strong>1.1 minutes elapse,</strong> during which the request is still marked as <em>pending</em> in the Network pane.</li>
  <li>The request is marked as complete in the browser's Network pane.</li>
  <li>The jQuery <code>xhr</code> fires success handlers</li>
  <li>Our custom API client's <a href="https://gist.github.com/3789008#L223">success handler</a> detects the 201 response code and fires off a subsequent GET request to the Location.</li>
  <li>The usual stuff happens, the GET responds promptly, and the outermost <code>$.Deferred()</code> object resolves, firing any associated client code success handlers.</li>
  </ol>
  
  <p><strong>Other details to consider:</strong></p>
  
  <ol>
  <li>GET requests and PUT requests in the same stack resolve promptly.</li>
  <li>When interacting directly with the outermost Nginx via a dedicated HTTP client, POST requests identical to the request in question resolve promptly.</li>
  <li>Removing the special-case 201 handler and subsequent GET has no effect on the bug.</li>
  <li>The delay is <em>always</em> 1.1 minutes. I've used <code>console.time()</code> to determine that the delay varies within the 65,000ms range.</li>
  <li>The delay only appears in this configuration. It does not occur in our development setups, which are slightly simpler.</li>
  </ol>
  
  <p><strong>Unverified assumptions I'm making:</strong></p>
  
  <ol>
  <li>Once Nginx logs a request, the response has been tied up with a bow and sent to the client with a handwritten thank-you note.</li>
  <li>This is not a bug in the browsers or in jQuery.</li>
  </ol>
  
  <p>Forgive the painstaking detail, but I've done my best to eliminate variables, and at present, I feel safe in saying that the problem is one of the following:</p>
  
  <ol>
  <li>a flaw in the physical structure of the universe</li>
  <li>a flaw in the perceptual models of our minds</li>
  <li>something else we haven't considered yet</li>
  </ol>
  
  <p>I'm hoping for #3. Any ideas?</p>