Nginx,Puma,Ubuntu 20.04 错误 111:连接被拒绝

Nginx, Puma, Ubuntu 20.04 error 111: Connection refused

当我尝试使用 NGINX 和 Puma 时,出现以下错误:

[error] 13416#13416: *3 connect() to unix:///home/deploy/app/tmp/pids/puma.sock failed (111: Connection refused) while connecting to upstream, client: ip.address.redacted, server: myapp.com, request: "GET / HTTP/1.1", upstream: "http://unix:///home/deploy/app/tmp/pids/puma.sock:/", host: "ip.address.redacted"

以下是我为设置此服务器所做的工作的简要说明:

当我 运行 sudo service puma status 我得到以下内容

● puma.service - Puma HTTP Server
     Loaded: loaded (/etc/systemd/system/puma.service; disabled; vendor preset: enabled)
     Active: activating (start) since Thu 2022-01-27 23:59:45 UTC; 28s ago
   Main PID: 1365 (bundle)
      Tasks: 12 (limit: 2274)
     Memory: 155.1M
     CGroup: /system.slice/puma.service
             └─1365 puma 4.3.10 (tcp://0.0.0.0:3000,unix:///home/deploy/app/tmp/pids/puma.sock) [app]

Jan 27 23:59:45 localhost systemd[1]: Starting Puma HTTP Server...
Jan 27 23:59:48 localhost rbenv[1365]: Puma starting in single mode...
Jan 27 23:59:48 localhost rbenv[1365]: * Version 4.3.10 (ruby 3.1.0-p0), codename: Mysterious Traveller
Jan 27 23:59:48 localhost rbenv[1365]: * Min threads: 5, max threads: 5
Jan 27 23:59:48 localhost rbenv[1365]: * Environment: production
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on tcp://0.0.0.0:3000
Jan 27 23:59:51 localhost rbenv[1365]: * Listening on unix:///home/deploy/app/tmp/pids/puma.sock
Jan 27 23:59:51 localhost rbenv[1365]: Use Ctrl-C to stop

ls -l /home/deploy/app/tmp/pids/puma.sock 产生:srwxrwxrwx 1 deploy users 0 Jan 28 00:05 /home/deploy/app/tmp/pids/puma.sock

这是在以下位置找到的 Nginx 配置文件:/etc/nginx/sites-enabled/default

upstream myapp {
  server unix:///home/deploy/app/tmp/pids/puma.sock;
}

server {
  listen 80;
  # server_name myapp.com;

  # ~2 seconds is often enough for most folks to parse HTML/CSS and
  # retrieve needed images/icons/frames, connections are cheap in
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;

  # path for static files
  root /home/deploy/app/public;
  access_log /home/deploy/app/log/nginx.access.log;
  error_log /home/deploy/app/log/nginx.error.log info;

  # this rewrites all the requests to the maintenance.html
  # page if it exists in the doc root. This is for capistrano's
  # disable web task
  if (-f $document_root/maintenance.html) {
    rewrite  ^(.*)$  /maintenance.html last;
    break;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;

    # If the file exists as a static file serve it directly without
    # running all the other rewrite tests on it
    if (-f $request_filename) {
      break;
    }

    # check for index.html for directory index
    # if it's there on the filesystem then rewrite
    # the url to add /index.html to the end of it
    # and then break to send it to the next config rules.
    if (-f $request_filename/index.html) {
      rewrite (.*) /index.html break;
    }

    # this is the meat of the rack page caching config
    # it adds .html to the end of the url and then checks
    # the filesystem for that file. If it exists, then we
    # rewrite the url to have explicit .html on the end
    # and then send it on its way to the next config rule.
    # if there is no file on the fs then it sets all the
    # necessary headers and proxies to our upstream pumas
    if (-f $request_filename.html) {
      rewrite (.*) .html break;
    }

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

  # Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
  # BUT there's a chance it could break the ajax calls.
  location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
     expires max;
     break;
  }

  # Error pages
  # error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /home/deploy/app/public;
  }

我已经向 nginx.conf 添加了一个用户指令,认为这将尝试 运行 作为部署用户的 NGINX 连接。但是,这没有任何效果。这是我的 Nginx.conf 文件

的前几行
user deploy;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

此时我不确定该怎么做。我对 Puma 没有任何经验,所以我不确定我是否在配置上做错了什么。据我所知 puma.sock 文件是自动生成的,我不确定如何更改 sock 文件的 permissions/owner 或者这是否是正确的做法。

有人遇到过这种情况吗?我在这里犯了什么错误?

我认为您在上游配置条目中的 / 过多。它正在尝试连接,就好像它是 HTTP url.

尝试更改:

upstream myapp {
  server unix:///home/deploy/app/tmp/pids/puma.sock;
}

upstream myapp {
  server unix:/home/deploy/app/tmp/pids/puma.sock;
}

来源:nginx ngx_http_upstream_module documentation