title: uwsgi + nginx + flask: upstream prematurely closed
body: <p>I created an endpoint on my flask which generates a spreadsheet from a database query (remote db) and then sends it as a download in the browser. Flask doesn't throw any errors. Uwsgi doesn't complain.</p>
  
  <p>But when I check nginx's error.log I see a lot of</p>
  
  <blockquote>
    <p>2014/12/10 05:06:24 [error] 14084#0: *239436 upstream prematurely
    closed connection while reading response header from upstream, client:
    34.34.34.34, server: me.com, request: "GET /download/export.csv HTTP/1.1", upstream: "uwsgi://0.0.0.0:5002", host: "me.com", referrer:
    "<a href="https://me.com/download/export.csv" rel="noreferrer">https://me.com/download/export.csv</a>"</p>
  </blockquote>
  
  <p>I deploy the uwsgi like</p>
  
  <pre><code>uwsgi --socket 0.0.0.0:5002 --buffer-size=32768 --module server --callab app
  </code></pre>
  
  <p>my nginx config:</p>
  
  <pre><code>server {
       listen 80;
       merge_slashes off;
       server_name me.com www.me.cpm;
  
       location / { try_files $uri @app; }
         location @app {
            include uwsgi_params;
            uwsgi_pass 0.0.0.0:5002;
            uwsgi_buffer_size 32k;
            uwsgi_buffers 8 32k;
            uwsgi_busy_buffers_size 32k;
       }
  
  }
  
  server {
        listen 443;
        merge_slashes off;
        server_name me.com www.me.com;
  
      location / { try_files $uri @app; }
         location @app {
            include uwsgi_params;
            uwsgi_pass 0.0.0.0:5002;
            uwsgi_buffer_size 32k;
            uwsgi_buffers 8 32k;
            uwsgi_busy_buffers_size 32k;
         }
  }
  </code></pre>
  
  <p>Is this an nginx or uwsgi issue, or both? </p>