[英]Connection Refused - Ruby on Rails App Deployed using Capistrano, Nginx, and Puma on Linode Server
我正在使用 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;
}
部署.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
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
中獲取此 output ,這似乎是正確的:
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,80,443/tcp (v6) ALLOW IN Anywhere (v6)
在這一點上,我不知道在哪里尋找根本問題。 有什么建議么?
編輯#2我現在注意到,如果我嘗試將 go 連接到瀏覽器中的 IP 地址,我會收到拒絕連接,但如果我嘗試使用 Z34D1F91FB2E514B8576FABDDR1A75A809A 連接時會收到連接:30 我不確定這是否相關。
根據您的 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如果我沒記錯的話,兩者都應該工作。
您的解決方案是將 puma 作為服務啟動,而不是使用 Capistrano 啟動它。 為此,您可以在此處關注 puma 的開發人員文檔: https://github.com/puma/puma/blob/master/docs/systemd.md 。
但是,IMO 您最好使用另一個應用程序服務器,例如Passenger,您不需要反向代理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.