Nginx rendering 502 error page - ruby-on-rails

I'm trying to configure nginx with puma to server my Ruby on Rails application. Whenever I visit my page I get a 502 bad gateway error.
/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
app recipe
# 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

You have nginx communicating with your puma server via socket
/home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock
You have puma creating this socket at
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
I can't see shared_path defined anywhere in your code though. Make sure that these two match (or just hardcode the path to eliminate variables for now).
I would also first check if /home/deploy/apps/puma-api/shared/tmp/sockets/puma-api-puma.sock even exists.

Related

Capistrano + puma + nginx: Capistrano puma is not creating puma-sockets and puma ids

Puma sockets and pids are not created after deploying with Capistrano. I have rails 5.1.2 application with ruby 2.4.0. Getting following error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
cap staging deploy:initial successfully completes. Have googled it and followed many links and answers but nothing is working. The deploy.rb and other files are following.
config/deploy.rb
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"
# Change these
server 'my_iP-address', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com'
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 :deploy_via, :remote_cache
set :deploy_to, "/home/deploy/#{fetch(:application)}"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
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 # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :bundle_binstubs, nil
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'
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
if fetch(:stage) == (:staging || 'staging')
if `git rev-parse HEAD` != `git rev-parse origin/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
elsif `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
Setting up branch, application, and stage in config/deploy/staging.rb
set :application, 'affiliate-staging'
set :deploy_to, "/home/deploy/#{fetch(:application)}"
set :stage, :staging
set :rails_env, :staging
set :branch, :staging
config/nginx.conf
upstream puma {
#using same puma socket name which I am sertting up in staging.rb
server unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/affiliate-staging/current/public;
access_log /home/deploy/affiliate-staging/current/log/nginx.access.log;
error_log /home/deploy/affiliate-staging/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 100M;
keepalive_timeout 10;
}
As you can see there should be pids and affiliate-saintlbeau-puma.sock but its not created. I am getting this error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
shared/puma.rb
#!/usr/bin/env puma
directory '/home/deploy/affiliate-staging/current'
rackup "/home/deploy/affiliate-staging/current/config.ru"
environment 'staging'
tag ''
pidfile "/home/deploy/shared/tmp/pids/puma.pid"
state_path "/home/deploy/shared/tmp/pids/puma.state"
stdout_redirect '/home/deploy/current/log/puma.error.log', '/home/deploy/current/log/puma.access.log', true
threads 4,16
bind 'unix:///home/deploy/shared/tmp/sockets/puma.sock'
workers 0
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = ""
end
before_fork do
ActiveRecord::Base.connection_pool.disconnect!
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
I am unable to understand what I am missing here. I hope I haven't messed up the question.

Rails app keeps crashing when experiencing heavy load

I was testing my app as I am anticipating a heavy load next week. I am currently using BlazeMeter to simulate the load. My server is hosted on AWS Elasticbeanstalk and RDS using m5.large EC2 instance. I have set up my application using Puma, Capistrano and Nginx. My config are as below:
//nginx.conf
upstream app {
server unix: ///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}
server {
listen 80;
server_name example.com;
root / home / deploy / apps / appname / current / public;
if ($http_x_forwarded_proto != "https") {
return 301 https: //$server_name$request_uri;
}
}
server {
listen 443 default_server http2;
root / home / deploy / apps / appname / current / public;
try_files / system / maintenance.html $uri / index.html $uri $uri.html #app;
access_log /
var / log / nginx / access.log main;
access_log /
var / log / nginx / healthd / application.log.$year - $month - $day - $hour healthd;
error_log /
var / log / nginx / error.log debug;
location / assets {
alias /
var / app / current / public / assets;
gzip_static on;
gzip on;
expires max;
add_header Cache - Control public;
}
location~ * \.(jpg | jpeg | gif | css | png | js | ico | svg | svgz) $ {
gzip_static on;
gzip on;
expires max;
add_header Cache - Control public;
}
location #app {
proxy_pass http: //app; # match the name of upstream directive which is defined above
proxy_set_header Host $http_host;
proxy_set_header X - Real - IP $remote_addr;
proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
proxy_set_header X - Forwarded - Proto $scheme;
proxy_set_header HTTP_CLIENT_IP $remote_addr;
}
error_page 500 502 503 504 / 500. html;
location / 500. html {}
error_page 404 / 404. html;
location / 404. html {}
error_page 422 / 422. html;
location / 422. html {}
}
##config/deploy.rb
# config valid only for current version of Capistrano
lock "3.8.2"
set :application, "appname"
set :repo_url, #"censored"
set :user, 'deploy'
set :puma_threads, [16, 64]
set :puma_workers, 2
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"#{}"/var/app/current"
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 # Change to false when not using ActiveRecord
append :linked_files, %w{config/database.yml}
append :linked_dirs, ".bundle", "tmp", "public/system"#, %w{bin log tmp/pids tmp/cache tmp/store vendor/bundle public/system}
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
end
There are many tests which failed and the error percentage is high. According to my logs from elasticbeanstalk,
[error] 2465#0: *2262 testing "/var/app/current/public/assets" existence failed (2: No such file or directory)
[crit] 2465#0: *2216 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream
I am a newbie to this and I have no idea why this is happening! Any help is appreciated! Thanks!
Update 1: My website also shows 502 Bad Gateway Nginx after reaching a certain number of users simulated.
Update 2:
Like what Myst has pointed out, I am using a database as well.
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
production:
<<: *default
adapter: mysql2
encoding: utf8
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
pool: 50 #20
timeout: 10000
Versions:
capistrano3-puma (3.1.0)
ruby (2.3.0)
rails (4.2.8)
mysql2 (0.4.9)
In general - when dealing with high load at first it's good to measure machine metrics - how much load can it take in terms of RPS/simultaneous clients while being stable. And during this test - LA, memory usage, IO to determine what resource is current bottleneck.
Set upstream connections limit in nginx (server unix:///... max_conns=20;), it is unlimited by default and under stress test this may cause workers to swell and get Out-of-memory errors.
Once workers are dead - nginx cannot connect to socket and reports error 502.
See if your puma_error_log (it looks that it is confused with access log in config) has exceptions.
Also since you only have 2 cpu cores - I doubt there's need for 64 threads times 2 workers, unless most of your requests result in waiting for calls to an external api.

Puma not running in production ec2

I am trying to deploy my website to amazon ec2 using nginx,puma and capistrano i followed this tutorial https://medium.com/#henslejoseph/deployment-a-rails-app-to-ec2-using-bitbucket-and-semaphoreci-d539bea90db3 the application got deployed to server but when i trying to access it via browser by going to website url it says This site can’t be reached.The next thing i tried is checking the nginx and the puma logs i dont find anythig in nginx logs but in puma_access.log there is error Errno::EADDRINUSE: Address already in use - bind(2) for "0.0.0.0" port 3000.After this i tried checking if any process is using the port 3000 using lsof -wni tcp:3000 and netstat -an | grep 3000 but the output was empty seems there is no process attached to port.
I am trying to debug it from 3 days but still clue less what is going.Any help is greatly appreciated I am just enclosing my all my config files for reference.
capfile
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/puma"
# require "capistrano/passenger"
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
deploy.rb
# config valid for current version and patch releases of Capistrano
lock "~> 3.10.1"
set :application, "myappname"
set :repo_url, "myapp_url"
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :branch, :deployment_work
# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, "/var/www/my_app_name"
set :deploy_to, '/home/yogesh/myapp_name'
# set :scm, :git
# Default value for :format is :airbrussh.
# set :format, :airbrussh
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
# append :linked_files, "config/database.yml", "config/secrets.yml"
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'config/application.yml')
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.3.3'
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
set :keep_releases, 3
# Default value for linked_dirs is []
# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
# Default value for keep_releases is 5
# set :keep_releases, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
# Pume config
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock" #accept array for multi-bind
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 8]
set :puma_workers, 0
set :puma_worker_timeout, nil
set :puma_init_active_record, true
set :puma_preload_app, false
production.rb
set :stage, :production
# Replace this EC2 server's public IP with your EC2 server's public IP
# Replace this user ('sarmad') with the one you created on your EC2 instance
server 'xx.xxx.xxx.xx', user: 'yogesh', roles: %w{web app db}
set :ssh_options,{ forward_agent: true, user: 'yogesh', keys: %w(~/.ssh/id_rsa) }
puma.rb
#!/usr/bin/env puma
directory '/home/yogesh/myapp_name/current'
rackup "/home/yogesh/myapp_name/current/config.ru"
environment 'production'
pidfile "/home/yogesh/myapp_name/shared/tmp/pids/puma.pid"
state_path "/home/yogesh/myapp_name/shared/tmp/pids/puma.state"
stdout_redirect '/home/yogesh/myapp_name/shared/log/puma_error.log', '/home/yogesh/myapp_name/shared/log/puma_access.log', true
threads 0,8
bind 'unix:///home/yogesh/myapp_name/shared/tmp/sockets/puma.sock'
workers 0
prune_bundler
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = ""
end
nginxconfig
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///home/yogesh/myapp_name/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /home/yogesh/myapp_name/current/public;
try_files $uri/index.html $uri #app;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection '';
proxy_pass http://app;
}
location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
gzip_static on;
expires max;
add_header Cache-Control public;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Can someone help me where i am going wrong have tried all stackoverflow questions and googled a lot from 3 days still no idea whats wrong with my config.
Update: After a lot of research i got the solution
all you have to do is ensure that the default file in sites_available folder should also be available in sites_enabled folder.This default file ensures that the website is enabled.If the default file is not available it seems like nginx is not listening on port 80.

Puma not creating socket file while deployed with Capistrano

I have followed the tutorial here on ow to deploy a Rails App using Capistrano and Puma. I had initially used with a Rails 4 and all worked fine, however I'm now trying with Rails 5 App and is not working. I've made some changes due to changes in Capistrano (I've also upgraded Capistrano) and now I'm stuck as the deployment does not seem to create the sock file while deploying and I get the following error:
*412 connect() to unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock failed (2: No such file or directory) while connecting to upstream, client: 172.245.92.157, server: api.myapp.com, request: "GET / HTTP/1.0", upstream: "http://unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock:/", host: "roboarticle.com"
Here's my Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
# Include default deployment tasks
# require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma
require 'capistrano/secrets_yml'
require 'capistrano/sidekiq'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Here's my deploy.rb:
# config valid only for current version of Capistrano
lock '3.8.0'
set :application, 'myapp'
# set :repo_url, 'git#example.com:me/my_repo.git'
# Change these
# server '178.62.117.38', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#gitlab.com:WagnerMatos/myapp.git'
set :application, 'myapp'
set :user, 'deployer'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, false
set :use_sudo, false
set :deploy_to, "/var/www/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 :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, false # Change to false when not using ActiveRecord
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
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/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
end
end
desc "reload the database with seed data"
task :seed do
puts "\n=== Seeding Database ===\n"
on primary :db do
within current_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:seed'
end
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'
# execute :bundle, 'exec cap production setup'
end
end
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
# before :starting, :check_revision
# after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
# after("deploy:compile_assets", "deploy:build_missing_paperclip_styles")
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
And here's my nguni config file:
upstream puma {
server unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 default_server deferred;
server_name api.myapp.io;
ssl on;
ssl_certificate /etc/nginx/ssl/api.myapp.io/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/api.myapp.io/api.myapp.io.key;
ssl_prefer_server_ciphers on;
root /var/www/apps/myapp/current/public;
access_log /var/www/apps/myapp/current/log/nginx.access.log;
error_log /var/www/apps/myapp/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;
proxy_read_timeout 300;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4000M;
keepalive_timeout 10;
}
Any ideas of what I'm missing here?

Error while connecting to upstream, Nginx and Unicorn, Mina , DigialOcean

I'm following this tutorial http://blog.mccartie.com/2014/08/28/digital-ocean.html to deploy my existing rails app to digital ocean with Mina, but my deploy fails, and looking into the errors logs I've found this error in the var/log/nginx/app_name.log:
connect() to unix:/home/deployer/rahmchicago/shared/sockets/unicorn.sock failed (2: No such file or directory) while connecting to upstream
These are my config files:
/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
etc/nginx/sites-enabled/default
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/home/deployer/YOUR_APP_NAME/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
# Application root, as defined previously
root /home/deployer/YOUR_APP_NAME/current/public;
server_name www.YOUR_APP_NAME.com YOUR_APP_NAME.com;
try_files $uri/index.html $uri #app;
access_log /var/log/nginx/YOUR_APP_NAME_access.log combined;
error_log /var/log/nginx/YOUR_APP_NAME_error.log;
location #app {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
config/unicorn.rb in the app
# Set your full path to application.
app_dir = File.expand_path('../../', __FILE__)
shared_dir = File.expand_path('../../../shared/', __FILE__)
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Fill path to your app
working_directory app_dir
# Set up socket location
listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
# Loging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"
# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"
before_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{app_dir}/Gemfile"
end
config/deploy.rb
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
require 'mina_sidekiq/tasks'
require 'mina/unicorn'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
set :domain, 'YOUR DROPLETS IP'
set :deploy_to, '/home/deployer/rahmchicago'
set :repository, 'YOUR GIT REPO URL'
set :branch, 'develop'
set :user, 'deployer'
set :forward_agent, true
set :port, '22'
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'log', 'config/secrets.yml']
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
queue %{echo "-----> Loading environment" #{echo_cmd %[source ~/.bashrc]} }
invoke :'rvm:use[ruby-2.0.0-p353#default]'
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .rbenv-version to your repository.
end
# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/log"]
queue! %[mkdir -p "#{deploy_to}/shared/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/config"]
queue! %[touch "#{deploy_to}/shared/config/database.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/database.yml'."]
queue! %[touch "#{deploy_to}/shared/config/secrets.yml"]
queue %[echo "-----> Be sure to edit 'shared/config/secrets.yml'."]
# sidekiq needs a place to store its pid file and log file
queue! %[mkdir -p "#{deploy_to}/shared/pids/"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/pids"]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
# stop accepting new workers
invoke :'sidekiq:quiet'
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
to :launch do
invoke :'unicorn:restart'
invoke :'sidekiq:restart'
end
end
end
I found that the deploy fails when mina execute this line invoke :'unicorn:restart' (in the deploy.rb file), all the other commands execute with success. Thanks for the help!

Resources