Skip to content

Instantly share code, notes, and snippets.

@purp
Last active January 5, 2017 04:40
Show Gist options
  • Save purp/75d9515c58d0758d4109db1ca86dbd02 to your computer and use it in GitHub Desktop.
Save purp/75d9515c58d0758d4109db1ca86dbd02 to your computer and use it in GitHub Desktop.
Setting up Rails 5, Puma, and nginx on OpenSUSE Leap 42.1

Huge thanks to @bounga for his post that didn't assume you've got a working /etc/nginx.conf. Everyone else's posts just assume that you've got the same /etc/nginx.conf as they do, and when you don't, you're screwed.

I like the sites-available/sites-enabled paradigm so I did this:

mkdir /etc/nginx/sites-available /etc/nginx/sites-enabled
ln -s /etc/nginx/sites-enabled /etc/nginx/vhosts.d

I highly recommend using capistrano-puma. Super basic steps condensed from the capistrano-puma readme:

  1. Add this to your Gemfile in the development group: gem 'capistrano3-puma', github: "seuros/capistrano-puma"
  2. Add this to your Capfile: require 'capistrano/puma' require 'capistrano/puma/nginx'
  3. Run cap puma:nginx_config to generate the two ERB templates in config/deploy/templates
  4. Add this to your config/deploy.rb: set :nginx_server_name, "YOUR_WEBSERVER_FQDN OTHER_ALIASES localhost"
  5. Uncomment this line in your config/deploy.rb: append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
  6. Replace the /etc/nginx.conf on your server with the one in this gist.
  7. Run cap production puma:nginx_config

I think that's all of it. I'll validate it another time and make this more complete. Maybe.

upstream puma_APPNAME_production {
server unix:/PATH/TO/RAILS/ROOT/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name YOUR_WEBSERVER_FQDN OTHER_ALIASES localhost;
root /PATH/TO/RAILS/ROOT/current/public;
try_files $uri/index.html $uri @puma_APPNAME_production;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
location @puma_APPNAME_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_APPNAME_production;
# limit_req zone=one;
access_log /PATH/TO/RAILS/ROOT/shared/log/nginx.access.log;
error_log /PATH/TO/RAILS/ROOT/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;
}
}
# Adapted from https://www.bounga.org/unix/2015/07/10/rails-nginx-puma/
#user www-data;
worker_processes 8;
#pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 5;
types_hash_max_size 2048;
server_names_hash_max_size 512;
server_names_hash_bucket_size 128;
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";
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment