title: upstream prematurely closed connection uwsgi nginx
body: <p>Good afternoon, colleagues.</p>
  
  <p>help with the solution of the problem (maybe even 2:))))</p>
  
  <p>nginx sends a mistake, though not always, and every 2-5 minutes
  tail -f /var/log/nginx/error.log</p>
  
  <pre><code>2015/03/29 12:15:28 [error] 22735#0: *219816 upstream prematurely closed connection while reading response header from upstream, client: 213.ххх.ххх.180, server: ххх, request: "POST /?uuid=a_4526e8e2-e0ac-426e-ac95-7a0c8ac37cc1 HTTP/1.1", upstream: "uwsgi://unix:///home/uwsgi-home/parsecdr/uwsgi.sock:", host: ххх
  </code></pre>
  
  <p>in logs uwsgi usually quiet and calm, but sometimes it bug</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
      response = self.get_response(request)
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 199, in get_response
      response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception
      return debug.technical_500_response(request, *exc_info)
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/views/debug.py", line 91, in technical_500_response
      html = reporter.get_traceback_html()
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/views/debug.py", line 350, in get_traceback_html
      return t.render(c)
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/template/base.py", line 148, in render
      return self._render(context)
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/template/base.py", line 142, in _render
      return self.nodelist.render(context)
    File "/home/uwsgi-home/local/lib/python2.7/site-packages/django/template/base.py", line 848, in render
      return mark_safe(''.join(bits))
  MemoryError
  </code></pre>
  
  <p>This error is the same error nginx as it happens and not (probably two different problems)</p>
  
  <p>Code - the parser xml. Files and benevolent 20-40 KB.
  The project is only one views and one URL.</p>
  
  <p>Most likely a problem with nginx or uwsgi confs</p>
  
  <p>Ask to see and tell. Actually configs:</p>
  
  <pre><code>[uwsgi]
  # Django-related settings
  # the base directory (full path)
  chdir           = /home/uwsgi-home/parsecdr/
  # Django's wsgi file
  module          = parsecdr.wsgi
  module          = parsecdr.wsgi:application
  module          = django.core.wsgi:get_wsgi_application()
  plugins         = python
  # the virtualenv (full path)
  home            = /home/uwsgi-home
  
  # process-related settings
  # master
  master          = true
  # maximum number of worker processes
  processes       = 10
  limit-as        = 192
  max-requests    = 5000
  harakiri    = 120
  buffer-size     = 49152
  # the socket (use the full path to be safe
  socket          = /home/uwsgi-home/parsecdr/uwsgi.sock
  # ... with appropriate permissions - may be needed
  #chmod-socket    = 666
  # clear environment on exit
  vacuum          = true
  touch-reload    = /home/uwsgi-home/parsecdr/touch-reload
  daemonize       = /var/log/uwsgi/parsecdr.log
  </code></pre>
  
  <p>и nginx
      upstream parsecdr {
          server unix:///home/uwsgi-home/parsecdr/uwsgi.sock; # for a file socket
      }</p>
  
  <pre><code>server {
      listen      80;
      server_name parsecdr.rt.ru;
      charset     utf-8;
  
      large_client_header_buffers 8 48k;
      client_max_body_size 50M;
      keepalive_timeout 75s;
      proxy_connect_timeout 60s;
      proxy_read_timeout 60s;
  
  
      location / {
          proxy_connect_timeout 300s;
          proxy_send_timeout   600s;
          proxy_read_timeout   600s;
          proxy_buffer_size    64k;
          proxy_buffers     16 32k;
          proxy_busy_buffers_size 64k;
          proxy_temp_file_write_size 64k;
  
  
          uwsgi_pass  parsecdr;
          include     /home/uwsgi-home/parsecdr/uwsgi.params;
      }
  }
  </code></pre>
  
  <p>Again, the error appears periodically.
  For every 50-60 records one error.
  And the second test may pass without errors</p>
  
  <p>Update:
  URL:</p>
  
  <pre><code>url(r'^$', xv.parse),
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>def parse(request):
      import urllib
      cdr_file = request.POST.get('cdr')
      cdr_file = urllib.unquote(cdr_file)
  
      cdr = XML_CDR()
      cdr.fs_host = request.META["REMOTE_ADDR"]
      cdr.parse_from_str(cdr_file)
      return HttpResponse(status=201)
  </code></pre>