连接被拒绝 - 在 Linode 服务器上使用 Capistrano、Nginx 和 Puma 部署的 Ruby on Rails 应用

如何解决连接被拒绝 - 在 Linode 服务器上使用 Capistrano、Nginx 和 Puma 部署的 Ruby on Rails 应用

我正在使用 Linode 部署 Rails 应用程序。我一直在关注本教程:https://matthewhoelter.com/2020/11/10/deploying-ruby-on-rails-for-ubuntu-2004.html

当我尝试访问我的网站时,我收到 ERR_CONNECTION_REFUSED。

我一直在调试这个问题。

nginx.conf

upstream puma {
  server unix:///home/deploy/apps/PhotographerFind/shared/tmp/sockets/PhotographerFind-puma.sock;
}

server {
  listen 80 default_server deferred;

  # If you're planning on using SSL (which you should),you can also go ahead and fill out the following server_name variable:
  # server_name example.com;

  # Don't forget to update these,too
  root /home/deploy/apps/PhotographerFind/current/public;
  access_log /home/deploy/apps/PhotographerFind/current/log/nginx.access.log;
  error_log /home/deploy/apps/PhotographerFind/current/log/nginx.error.log info;

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

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

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

deploy.rb

# # config valid for current version and patch releases of Capistrano
# lock "~> 3.15.0"

# Change these
server 'ip.address.here',port: 22,roles: [:web,:app,:db],primary: true

set :repo_url,'git@ssh.dev.azure.com:v3/gitrepo/myrepo/myrepo'
set :application,'PhotographerFind'

# If using Digital Ocean's Ruby on Rails Marketplace framework,your username is 'rails'
set :user,'deploy'
set :puma_threads,[4,16]
set :puma_workers,0

# Don't change these unless you know what you're doing
set :pty,true
set :use_sudo,false
set :stage,:production
set :deploy_via,:remote_cache
set :deploy_to,"/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind,"unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state,"#{shared_path}/tmp/pids/puma.state"
set :puma_pid,"#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log,"#{release_path}/log/puma.access.log"
set :puma_error_log,"#{release_path}/log/puma.error.log"
set :ssh_options,{ forward_agent: true,user: fetch(:user),keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app,true
set :puma_worker_timeout,nil
set :puma_init_active_record,true  # Change to false when not using ActiveRecord
    
namespace :puma do
    desc 'Create Directories for Puma Pids and Socket'
    task :make_dirs do
      on roles(:app) do
        execute "mkdir #{shared_path}/tmp/sockets -p"
        execute "mkdir #{shared_path}/tmp/pids -p"
      end
    end
  
    before :start,:make_dirs
  end
  
  namespace :deploy do
    desc "Make sure local git is in sync with remote."
    task :check_revision do
      on roles(:app) do
        unless `git rev-parse HEAD` == `git rev-parse origin/master`
          puts "WARNING: HEAD is not the same as origin/master"
          puts "Run `git push` to sync changes."
          exit
        end
      end
    end
  
    desc 'Initial Deploy'
    task :initial do
      on roles(:app) do
        before 'deploy:restart','puma:start'
        invoke 'deploy'
      end
    end
  
    desc 'Restart application'
    task :restart do
      on roles(:app),in: :sequence,wait: 5 do
        invoke 'puma:restart'
      end
    end
  
    before :starting,:check_revision
    after  :finishing,:compile_assets
    after  :finishing,:cleanup
  end
  
  set :linked_files,fetch(:linked_files,[]).push("config/master.key")
  

puma.access.log

Puma starting in single mode...
* Version 3.12.6 (ruby 2.6.3-p62),codename: Llamas in Pajamas
* Min threads: 4,max threads: 16
* Environment: production
* Daemonizing...
=== puma startup: 2021-02-18 06:23:44 +0000 ===
=== puma startup: 2021-02-18 06:26:53 +0000 ===
* Listening on unix:///home/deploy/apps/PhotographerFind/shared/tmp/sockets/PhotographerFind-puma.sock

puma.error.log

=== puma startup: 2021-02-18 06:23:44 +0000 ===
/home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:395:in `for_fd': Bad file descriptor - fstat(2) (Errno::EBADF)
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:395:in `inherit_unix_listener'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:115:in `block in parse'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:90:in `each'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:90:in `parse'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/runner.rb:153:in `load_and_bind'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/single.rb:98:in `run'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/launcher.rb:186:in `run'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/cli.rb:80:in `run'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/bin/puma:10:in `<top (required)>'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/bin/puma:23:in `load'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/bin/puma:23:in `<main>'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/bin/ruby_executable_hooks:24:in `eval'
        from /home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/bin/ruby_executable_hooks:24:in `<main>'
=== puma startup: 2021-02-18 06:26:53 +0000 ===

我认为这可能指向了问题,但似乎不是每次 puma 启动时都会出现,所以我不太确定这里发生了什么。


更新

我认为可能与端口有关,但这是 sudo netstat -ntlp | grep LISTEN

的输出
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7307/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      963/sshd            
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      955/postgres        
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      3892/java           
tcp6       0      0 ::1:9200                :::*                    LISTEN      3892/java           
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      3892/java           
tcp6       0      0 ::1:9300                :::*                    LISTEN      3892/java           
tcp6       0      0 :::22                   :::*                    LISTEN      963/sshd            
tcp6       0      0 :::5432                 :::*                    LISTEN      955/postgres        

这是/var/log/nginx/error.log中的内容:

2021/02/18 23:09:58 [notice] 7325#7325: signal process started

我注意到 Puma 只在重启时抛出错误文件描述符。它似乎正在运行。这是puma.access.log的内容:

=== puma startup: 2021-02-18 23:04:28 +0000 ===
[6971] - Worker 0 (pid: 6973) booted,phase: 0
[6971] - Worker 1 (pid: 6979) booted,phase: 0

我认为这可能是端口问题,并从 sudo ufw status numbered 获取此输出,这似乎是正确的:

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 80,443/tcp                 ALLOW IN    Anywhere                  
[ 2] 22,80,443/tcp              ALLOW IN    Anywhere                  
[ 3] 80,443/tcp (v6)            ALLOW IN    Anywhere (v6)             
[ 4] 22,443/tcp (v6)         ALLOW IN    Anywhere (v6) 

此时我不知道还能在哪里寻找根本问题。有什么建议吗?

编辑 #2 我现在注意到,如果我尝试访问浏览器中的 IP 地址,我会收到连接被拒绝,但如果我尝试访问 IPADDRESS:3000,我会收到连接超时。我不确定这是否相关。

解决方法

该错误与您的套接字文件有关,如您的 puma.error.log 所示:

/home/deploy/apps/PhotographerFind/shared/bundle/ruby/2.6.0/gems/puma-3.12.6/lib/puma/binder.rb:395:in `for_fd': Bad file descriptor - fstat(2) (Errno::EBADF)

您可以尝试以下操作,仅确认套接字文件有问题:

  • 使用 -w 1 以单工模式启动 puma
  • nginx 和 puma 之间使用端口而不是套接字进行通信

如果我没记错的话,两者都应该可以工作。

您的解决方案是将 puma 作为服务启动,而不是使用 Capistrano 启动。为此,您可以在此处查看 puma 的开发人员文档:https://github.com/puma/puma/blob/master/docs/systemd.md

但是,IMO 最好使用另一个应用程序服务器,例如Passenger,您不需要反向代理。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res