title: Nginx reverse proxy uwsgi django intermittent 502
body: <p>I have a site setup with Nginx as load balancer (least_conn) that uses a seperate uwsgi/django server as its upstream. This is normally a pool of about 5 uwsgi/django servers but I've limited it to just one to make sure it's not a bad server. </p>
  
  <p>Under 'normal' user behavior, everything appears to be working fine. 
  My problem is that rapid, successive requests will intermittently generate a 502 error. I can recreate this with a few tries by opening a page in our admin that contains a list to all the articles. By opening a bunch of the links in new tabs, about 1 in 10 will fail with a 502 error. </p>
  
  <p>Another curiosity, the 502s happen very quickly when they do happen. For example, when a 502 does show up it's as instantaneous as the tab opening.  Where I would expect the nginx frontend to wait a bit before coming back with an error. </p>
  
  <p>I'm using uwsgi_pass to directly proxy between the nginx load balancer and uwsgi. I've read in a number of other posts about having to increase buffers. I've tried setting uwsgi_buffers_size 16k; and uwsgi_buffers 4 16k; but they have not made a difference. </p>
  
  <p>Error logs from both Nginx and Uwsgi don't show anything about the 502s. Google webmaster tools, does, however notice them.  </p>
  
  <p>Relevant nginx.conf settings:</p>
  
  <pre><code>worker_processes  auto;
  
  error_log  logs/error.log;
  error_log  logs/error.log  notice;
  error_log  logs/error.log  info;
  
  events {
      multi_accept on;
      use epoll;
      worker_connections  1024;
  }
  
  
  http {
      include       mime.types;
      default_type  application/octet-stream;
  
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  
      access_log  off;
  
      sendfile        on;
      tcp_nopush     on;
  
  
  
      #Timeouts
      keepalive_timeout  65;
      send_timeout 60;
      client_body_timeout 12;
      client_header_timeout 12;
  
      upstream backend {
      least_conn;
      server ipaddress:22882;
      }
  
      server {
          listen       27313;
          server_name  exampledomain.com;
  
  
          access_log  off;
  
          port_in_redirect off;
  
          uwsgi_next_upstream error;
  
          location / {
              uwsgi_read_timeout 300;
              uwsgi_send_timeout 150;
              uwsgi_connect_timeout 300;
              uwsgi_buffer_size 16k;
              uwsgi_buffers 4 16k;
              uwsgi_pass backend;
          }
  </code></pre>
  
  <p>Uwsgi started with these parameters:</p>
  
  <pre><code>--socket :22882 
  --master 
  --workers 5 
  --threads 2 
  --max-requests 1500 
  --wsgi-file ~/django_prod/wsgi.py 
  --pidfile ~/django_prod/nginx/log/uwsgi.pid 
  --daemonize ~/django_prod/nginx/log/uwsgi.log 
  --python-path ~/django_prod 
  --python-path ~/django_prod/lib/ 
  --env DJANGO_SETTINGS_MODULE=myproject.settings 
  --vacuum
  </code></pre>