Nginx 渲染 502 错误页面

Posted

技术标签:

【中文标题】Nginx 渲染 502 错误页面【英文标题】:Nginx rendering 502 error page 【发布时间】:2016-04-16 01:27:07 【问题描述】:

我正在尝试使用 puma 配置 nginx 来为我的 Ruby on Rails 应用程序提供服务。每当我访问我的页面时,都会收到 502 bad gateway 错误。

/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

部署.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

【问题讨论】:

Nginx 具有非常棒(而且非常嘈杂)的调试可能性,可用于调查 nginx 中的问题,例如 rewrite_log。 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 Make确保 nginx 进程的用户有权从套接字读取,当然,套接字已启动并正在运行。 @MathewB。这似乎是一个权限问题。将 nginx 用户添加到特权组后,我仍然收到相同的错误 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: , 如果我将上游更改为 server localhost:500 并启动 forman,我就能让它工作。但是我在让工头开始使用 capistrano 时遇到了问题。 【参考方案1】:

你有 nginx 通过套接字与你的 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 中声明的值吗?如果有怎么办? 如果这是你的价值,那么这可能就是为什么没有任何工作。 Puma 正在/var/www/puma-api/shared/tmp/sockets/??.sock 创建套接字,而 nginx 正在尝试从 /home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock 读取它 我能够通过将 /home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock 替换为 localhost:5000 并将 puma 替换为工头来使其工作。

以上是关于Nginx 渲染 502 错误页面的主要内容,如果未能解决你的问题,请参考以下文章

nginx自定义500,502,504错误页面无法跳转

nginx + gunicorn 502 网关错误

nginx配置错误页面

113资讯网——NGINX 502 Bad Gateway——解决方案

Nginx 用 502 Bad Gateway 覆盖一般的 symfony 错误

服务器出现 nginx 502 Bad Gateway