Nginx / Puma / Rails / EC2间歇性地抛出502 Bad Gateway

时间:2017-05-16 19:02:12

标签: ruby-on-rails nginx amazon-ec2 puma

有时我的应用会抛出502。起初我以为这是一个内存问题,所以我从1GB主机转移到4GB主机,但它仍然在发生。

我也启用了HTTPS。关于可能出现什么问题的想法?

这些是我的配置:

puma.rb:

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
port        ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "production" }
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
stdout_redirect '/var/www/hummingprice/log/puma-stdout', '/var/www/hummingprice/log/puma-stderr', true
bind 'unix:///var/www/myapp/tmp/pids/puma.sock

nginx的:

upstream myapp {
  server unix:///var/www/myapp/tmp/pids/puma.sock;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;


        root /var/www/myapp/public;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name myapp.com;
        client_max_body_size 5M;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header  Host $http_host;
                proxy_pass_header HTTP_X_AUTH_TOKEN;
                proxy_redirect off;
                proxy_read_timeout 150;

                if (!-f $request_filename) {
                        proxy_pass http://myapp;
                        break;
                }
        }

        location ^~ /assets/ {
            gzip_static on;
            expires max;
            add_header Cache-Control public;
        }

        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot

        if ($scheme != "https") {
            return 301 https://$host$request_uri;
        } # managed by Certbot
}

我在Nginx日志中看到了很多这些错误:

2017/05/16 18:43:13 [error] 2893#0: *43 connect() to unix:///var/www/myapp/tmp/pids/puma.sock failed (111: Connection refused) while connecting to upstream, client: 24.143.97.132, server: adintern.com, request: "GET / HTTP/1.1", upstream: "http://unix:///var/www/myapp/tmp/pids/puma.sock:/", host: "myapp.com"

2017/05/16 18:50:35 [error] 3392#0: *37 upstream prematurely closed connection while reading response header from upstream, client: 175.110.119.142, server: adintern.com, request: "POST /xxx HTTP/1.1", upstream: "http://unix:///var/www/myapp/tmp/pids/puma.sock:/xxx", host: "adintern.com", referrer: "https://myapp.com/xxx"

我也在Puma的stderr中看到了seg错误:

/usr/local/rvm/gems/ruby-2.3.3/gems/activesupport-4.1.8/lib/active_support/core_ext/string/output_safety.rb:21: [BUG] Segmentation fault at 0x00000000000039 ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]

0 个答案:

没有答案