【问题标题】:Nginx, Puma, Ubuntu 20.04 error 111: Connection refusedNginx、Puma、Ubuntu 20.04 错误 111:连接被拒绝
【发布时间】:2022-01-30 04:20:50
【问题描述】:

当我尝试使用 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"

这里只是我为设置此服务器所做的快速细分:

  • 我正在使用 RBENV
  • 我使用的是 Ruby 3.1.0 和 Rails 6.1
  • 我没有使用 Capistrano
  • 此服务器设置为禁用 root 访问,而是使用名为 deploy 的 sudo 特权用户

当我运行 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 (.*) $1/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 (.*) $1.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 文件的权限/所有者,或者这是否是正确的做法。

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

【问题讨论】:

    标签: ruby-on-rails ubuntu nginx puma


    【解决方案1】:

    我认为您在上游配置条目中有太多 / 。它正在尝试连接,就好像它是一个 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

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 2014-09-13
      • 2017-09-08
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多