title: Rails app running on puma and nginx keeps dying every few hours with Bad Gateway
body: <p>I have a rails app that I just deployed to Digital Ocean and it's running on Puma and Nginx.</p>
  
  <p>Eventually all it returns is a bad gateway and this is what is in the error.log</p>
  
  <pre><code>2014/09/09 22:23:06 [error] 5729#0: *3059 connect() to unix:///var/www/mysite/mysite_app.sock failed (111: Connection refused) while connecting to upstream, client: 67.5.19.192, server: mysite.com, request: "GET / HTTP/1.1", upstream: "http://unix:///var/www/mysite/mysite_app.sock:/", host: "mysite.com"
  </code></pre>
  
  <p>To fix it, I just restart puma and it seems to work.</p>
  
  <p>How can I debug this to figure out why it keeps dying?</p>
  
  <p>Here's my nginx config:</p>
  
  <pre><code>upstream mysite {
                  server unix:///var/www/mysite/mysite_app.sock;
          }
          server {
                  listen 80;
                  server_name mysite.com;
                  root /var/www/mysite/current/public;
          client_max_body_size 20M;
  
                  location / {
                          proxy_pass http://mysite; # match the name of upstream directive which is defined above
                          proxy_set_header Host $host;
                          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                  }
                  location ~* ^/assets/ {
                          # Per RFC2616 - 1 year maximum expiry
                          expires 1y;
                          add_header Cache-Control public;
  
                          # Some browsers still send conditional-GET requests if there's a
                          # Last-Modified header or an ETag header even if they haven't
                          # reached the expiry date sent in the Expires header.
                          add_header Last-Modified "";
                          add_header ETag "";
                          break;
                  }
          }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Could this be caused from running out of memory?</p>
  
  <p>Here's my current state of memory, but as I keep running this command every so often, the amount of free memory goes down and once i restart puma it jumps back up to like 150.</p>
  
  <pre><code>$ free -m
               total       used       free     shared    buffers     cached
  Mem:           490        440         50          0         17         84
  -/+ buffers/cache:        338        151
  Swap:            0          0          0
  </code></pre>