Skip to content

Instantly share code, notes, and snippets.

@allanbatista
Created December 18, 2016 19:18
Show Gist options
  • Save allanbatista/d8964176ac512ec6653a960fb030a0c1 to your computer and use it in GitHub Desktop.
Save allanbatista/d8964176ac512ec6653a960fb030a0c1 to your computer and use it in GitHub Desktop.
Puma + nginx for rails
# /etc/nginx/sites-enabled/default
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///var/www/html/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
root /var/www/html/current/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Request-Start "t=${msec}";
proxy_set_header Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
location /admin/sidekiq {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app;
}
}
# /etc/init/puma.conf
description "Puma"
start on runlevel [2]
stop on runlevel [016]
# change to match your deployment user
setuid root
setgid root
respawn
script
exec /bin/bash <<'EOT'
source /usr/local/rvm/bin/rvm
cd /var/www/html/current
RAILS_ENV=production bundle exec puma -C /var/www/html/current/config/puma.rb
EOT
end script
# config/puma.rb
cpu_count = `cat /proc/cpuinfo | grep processor | wc -l`.to_i
workers cpu_count
threads 20, 20
app_dir = "/var/www/html/current"
rails_env = "production"
environment rails_env
bind "unix:///var/www/html/shared/sockets/puma.sock"
pidfile "/var/www/html/shared/pids/puma.pid"
state_path "/var/www/html/shared/pids/puma.state"
activate_control_app
stdout_redirect '/var/www/html/shared/log/puma.log', '/var/www/html/shared/log/puma.log', true
on_worker_boot do
end
before_fork do
require "puma_worker_killer"
free_memory_mb = `cat /proc/meminfo | grep MemAvailable`.gsub(/.*:/, "").to_i / 1024
PumaWorkerKiller.config do |config|
config.ram = free_memory_mb
config.frequency = 60
config.percent_usage = 0.98
config.rolling_restart_frequency = 3600
config.reaper_status_logs = true
end
PumaWorkerKiller.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment