Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails app running on puma and nginx keeps dying every few hours with Bad Gateway

I have a rails app that I just deployed to Digital Ocean and it's running on Puma and Nginx.

Eventually all it returns is a bad gateway and this is what is in the error.log

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"

To fix it, I just restart puma and it seems to work.

How can I debug this to figure out why it keeps dying?

Here's my nginx config:

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;
                }
        }

EDIT

Could this be caused from running out of memory?

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.

$ free -m
             total       used       free     shared    buffers     cached
Mem:           490        440         50          0         17         84
-/+ buffers/cache:        338        151
Swap:            0          0          0
like image 329
Catfish Avatar asked Sep 10 '14 02:09

Catfish


2 Answers

It seems this is actually an issue with ruby 2.1 (specifically i'm using 2.1.2) and its garbage collection.

A google search like this seems to have lots of various threads on it http://bit.ly/1s2vBC0

Here's a ruby bug ticket on the issue: https://bugs.ruby-lang.org/issues/9607

like image 200
Catfish Avatar answered Nov 05 '22 21:11

Catfish


Lack of memory can be issue, but you better look into puma and rails logs, not nginx only. In the application folder:

tail -f log/puma*
tail -f log/production.log
like image 38
Aleksey Deryagin Avatar answered Nov 05 '22 21:11

Aleksey Deryagin