Rails 5 个带有 Puma 和 Nginx 的应用程序 - 111:连接到上游、客户端时连接被拒绝

Rails 5 app with Puma and Nginx - 111: Connection refused while connecting to upstream, client

我收到这个错误:

2016/09/29 01:05:39 [error] 7169#0: *3 connect() to unix:/home/deploy/tasks/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: 99.254.197.158, server: localhost, request: "GET / HTTP/1.1", upstream: "http://unix:/home/deploy/tasks/shared/tmp/sockets/puma.sock:/", host: "ec2-54-88-181-57.compute-1.amazonaws.com"

尝试将此 URL 用于我的应用程序时:

http://ec2-54-88-181-57.compute-1.amazonaws.com/

浏览器也显示此消息:

We're sorry, but something went wrong.
If you are the application owner check the logs for more information.

但是,当我直接在端口 3000 上使用 Puma 时,我可以访问我的应用程序 URL:

http://ec2-54-88-181-57.compute-1.amazonaws.com:3000/

而且我可以通过这种方式浏览应用程序的所有页面。

这是我的一些配置文件:

$ ls -l /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 root root 34 Sep 28 22:46 default -> /etc/nginx/sites-available/default


$ sudo cat /etc/nginx/nginx.conf
[sudo] password for deploy: 
user root; #www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


$ sudo cat /etc/nginx/sites-available/default
upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:/home/deploy/tasks/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name localhost;

    root /home/deploy/tasks/current/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

服务器正在侦听端口 80、22 和 3000(对于 Puma)

$ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN     
tcp        0    280 172.31.51.143:22        99.254.197.158:60843    ESTABLISHED
tcp        0      0 172.31.51.143:22        99.254.197.158:60842    ESTABLISHED
tcp        0      0 172.31.51.143:59545     172.31.47.0:5432        ESTABLISHED
tcp        0      0 172.31.51.143:59544     172.31.47.0:5432        ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
udp        0      0 0.0.0.0:55159           0.0.0.0:*                          
udp        0      0 0.0.0.0:68              0.0.0.0:*                          
udp6       0      0 :::12784                :::*                               

Nginx 和 Puma 是 运行。

$ ps -ef | grep nginx
root      1644  1586  0 01:21 pts/0    00:00:00 sudo tail -f /var/log/nginx/error.log
root      1645  1644  0 01:21 pts/0    00:00:00 tail -f /var/log/nginx/error.log
root      1698     1  0 01:39 ?        00:00:00 nginx: master process /usr/sbin/nginx
root      1701  1698  0 01:39 ?        00:00:00 nginx: worker process
root      1702  1698  0 01:39 ?        00:00:00 nginx: worker process
root      1703  1698  0 01:39 ?        00:00:00 nginx: worker process
root      1704  1698  0 01:39 ?        00:00:00 nginx: worker process
deploy    1736  1309  0 02:13 pts/1    00:00:00 grep nginx


$ ps -ef | grep puma
deploy    1564     1  0 01:20 ?        00:00:00 puma 3.6.0 (tcp://0.0.0.0:3000) [20160928212850]                                                                                                                                                                                                                                                                                                                                                      
deploy    1571  1564  0 01:20 ?        00:00:01 puma: cluster worker 0: 1564 [20160928212850]    

我正在使用 Capistrano 部署到 AWS EC2 Ubuntu 14.04 服务器。 没有与部署相关的错误。

我获取了所有博客/post 我找到了,但是到目前为止 none 这些解决方案对我有用。

为了让 Nginx 服务器正常工作,接下来我应该尝试什么?

我废弃了 AWS EC2 实例并使用 Ubuntu 14.04 实施重新创建它,我升级到 16.04。我严格遵循此处的指导

http://codepany.com/blog/rails-5-puma-capistrano-nginx-jungle-upstart/ 以及来自同一博客的相关 link。

现在 Nginx 和 Puma 一起正常工作,我的应用程序在这里运行完美:

http://ec2-54-159-156-217.compute-1.amazonaws.com/ 指南的唯一区别是我为数据库保留了 AWS RDS 实例。尽管我在 Mac 上使用 RBENV,但我在生产服务器中使用了 RVM。我使用 ubuntu 用户(如 root)进行部署,因为我怀疑我遇到的所有问题都与权限有关,而且我不知道如何修复它们。

之前尝试在 socket 上正确启动 Puma 并使其与 Nginx 一起工作时遇到许多错误,尤其是在

之后没有重新启动

上限生产部署 与生成秘密并将此值放入适当的文件有关。对我来说,将它写在 /etc/environment 文件中效果最好。

我还对文件 /etc/ssh/sshd_config 进行了更改,以便通过 ssh 获得 root 或 ubuntu 访问权限。在这件事上link

https://forums.aws.amazon.com/thread.jspa?threadID=86876 很有用。