title: 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream with flask, uWSGI, nginx
body: <p>how are you?
  I have a server on DigitalOcean with Ubuntu 16.04
  I am making a Flask Application with uWSGI and Nginx.</p>
  
  <p>My files look like:
  wsgi.py:</p>
  
  <pre><code>from flaskapp import app
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p>flaskapp.ini</p>
  
  <pre><code>[uwsgi]
  limit-as = 512
  module = wsgi:app
  plugin = python
  master = true
  processes = 5
  socket = flaskapp.sock
  chmod-socket = 660
  vacuum = true
  die-on-term = true
  </code></pre>
  
  <p>/etc/systemd/system/flaskapp.service</p>
  
  <pre><code>[Unit]
  Description=uWSGI instance to serve flaskapp
  After=network.target
  
  [Service]
  User=blitwak
  Group=www-data
  WorkingDirectory=/home/blitwak/flaskapp
  Environment="PATH=/home/blitwak/flaskapp/myprojectenv/bin"
  ExecStart=/home/blitwak/flaskapp/myprojectenv/bin/uwsgi --ini flaskapp.ini
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>/etc/nginx/sites-available/flaskapp.conf</p>
  
  <pre><code>server {
      listen 80;
      server_name 162.243.76.55;
  
      charset utf-8;
      proxy_buffering on;
      proxy_buffer_size 1k;
      proxy_buffers 24 4k;
      proxy_busy_buffers_size 8k;
      proxy_max_temp_file_size 2048m;
      proxy_temp_file_write_size 32k;
  
      proxy_connect_timeout 75s;
      proxy_read_timeout 300s;
  
  
      location / {
          include uwsgi_params;
          uwsgi_pass unix:///home/blitwak/flaskapp/flaskapp.sock;
  
   uwsgi_param UWSGI_SCRIPT app.wsgi;
  
            uwsgi_buffer_size 32k;
            uwsgi_buffers 8 32k;
            uwsgi_busy_buffers_size 32k;
  
  
     }
  }
  </code></pre>
  
  <p>and in /var/log/nginx/error.log it says a lot of times:</p>
  
  <blockquote>
    <p>2017/01/19 15:58:18 [error] 19181#19181: *36 upstream prematurely
    closed connection while reading response header from upstream, client:
    {myip}, server: {myserver}, request: "POST /yajugue HTTP/1.1",
    upstream: "uwsgi://unix:///home/blitwak/flaskapp/flaskapp.sock:",
    host: "{myserver}", referrer: "http://{myserver}/jugarPrimeraVez"</p>
  </blockquote>
  
  <p>Thank you very much!!!</p>
  
  <p>How can I resolve it?</p>