Nginx 渲染502错误页面

3
我正在尝试使用nginx和puma配置我的Ruby on Rails应用程序。每当我访问我的页面时,我都会收到502错误网关错误。

/etc/nginx/sites-enabled/puma-api

upstream puma_puma-api_production { 
  server unix:/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock fail_timeout=0;
}
server {
  listen  80 default;
  client_max_body_size 4G;
  keepalive_timeout 10;
  error_page 500 502 504 /500.html;
  error_page 503 @503;
  root /home/deploy/apps/puma-api/current/public;
  try_files $uri/index.html $uri @puma_puma-api_production;
  location @puma_puma-api_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://puma_puma-api_production;
    # limit_req zone=one;
    access_log /home/deploy/apps/puma-api/shared/log/nginx.access.log;
    error_log /home/deploy/apps/puma-api/shared/log/nginx.error.log;
  }
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  location = /50x.html {
    root html;
  }
  location = /404.html {
    root html;
  }
  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }
  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }
  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  location ~ \.(php|html)$ {
    return 405;
  }
}

/etc/nginx/nginx.conf

user nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {

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


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

  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;

  keepalive_requests 100;
  keepalive_timeout  65;


  gzip  on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_vary off;
  gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
  gzip_min_length  1000;
  gzip_disable     "MSIE [1-6]\.";


  variables_hash_max_size 1024;
  variables_hash_bucket_size 64;
  server_names_hash_bucket_size 64;
  types_hash_max_size 2048;
  types_hash_bucket_size 64;



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

app/config/puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 5000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end

deploy.rb

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'puma-api'
set :repo_url, 'git@github.com:chaione/puma-api.git'
set :user, 'deploy'

# set :nginx_domains, 'localhost'
# set :app_server_socket, "#{shared_path}/sockets/puma-#{fetch :application}.sock"

# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name'

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.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.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

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
  after :finishing,    :restart
end


# # Default value for :linked_files is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/images')
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

namespace :deploy do
  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end
end

应用程序配方

# create www directory
directory '/var/www' do
  user node['user']['name']
  group node['group']
  mode 0755
end

# create shared directory structure for app
path = "/var/www/#{node['app']}/shared/config"
execute "mkdir -p #{path}" do
  user node['user']['name']
  group node['group']
  creates path
end

# create database.yml file
template "#{path}/database.yml" do
  source 'database.yml.erb'
  mode 0640
  owner node['user']['name']
  group node['group']
end

1
Nginx拥有非常出色(也非常吵闹)的调试功能,可以用于调查nginx内部的问题,例如rewrite_log。 - Smar
当Nginx无法与您的后端代理通信时,通常会返回HTTP 502错误,在您的情况下是server unix:/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock fail_timeout=0;确切的错误将写入错误日志文件,在您的情况下为/var/log/nginx/error.log确保Nginx进程的用户具有从套接字读取的权限,并且当然,套接字正在运行。 - Mathew B.
@MathewB。看起来这是一个权限问题。将nginx用户添加到特权组后,我仍然收到相同的错误failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: , - Antarr Byrd
如果我将上游更改为 server localhost:500 并启动 forman,我可以使其正常工作。但是我在 capistrano 中启动 foreman 时遇到了问题。 - Antarr Byrd
1个回答

2
您的nginx通过socket与puma服务器通信。
/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock

您正在创建该套接字的Puma实例

set :puma_bind,       "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"

我在你的代码中没有看到shared_path的定义。请确保这两个匹配(或者暂时硬编码路径来消除变量)。

我还建议先检查/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock是否存在。


我还有点困惑。在我的应用程序配方中,共享路径被定义为/var/www/puma-api/shared/。这是一个需要在deploy.rb中声明的值吗?如果是,该如何声明? - Antarr Byrd
如果这是你的价值观,那可能就是为什么什么都不起作用的原因。Puma正在创建套接字/var/www/puma-api/shared/tmp/sockets/??.sock,而nginx正在尝试从/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock读取它。 - Martin Konecny
我通过将“/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock”替换为“localhost:5000”,并使用foreman启动puma,成功使其工作。 - Antarr Byrd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接