Cannot Start Puma Ubuntu 16.04 - ruby-on-rails

Hi i followed this thread of digitalocean to deploy my RubyOnRails app on digitalocean vps
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
My Config:
512 MB RAM(with 1 gb swap)
ubuntu 16.04
Ruby2.33(with rbenv)
This tutorial lists usage of upstart but as i searched i found ubuntu 16.04 uses systemd.
I found this thread but still could start Puma Server
https://github.com/puma/puma/issues/1211
when i run which puma it gives
/home/ashish/.rbenv/shims/puma
Also here is my puma.service file
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=www-data
# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/var/www/mystore.rentcallcenter.com
# Helpful for debugging socket activation, etc.
Environment=PUMA_DEBUG=1
# The command to start Puma. This variant uses a binstub generated via
# `bundle binstubs puma --path ./sbin` in the WorkingDirectory
# (replace "<WD>" below)
ExecStart=/home/ashish/.rbenv/shims/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
# Variant: Use config file with `bind` directives instead:
# ExecStart=<WD>/sbin/puma -C config.rb
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
Restart=always
[Install]
WantedBy=multi-user.target
and here is my puma.rb in rails config folder
# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads 1, 6
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
#port ENV.fetch("PORT") { 3000 }
# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "production" }
# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
workers ENV.fetch("WEB_CONCURRENCY") { 1 }
# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory. If you use this option
# you need to make sure to reconnect any threads in the `on_worker_boot`
# block.
#
# preload_app!
# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
# before_fork do
# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
# end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
# on_worker_boot do
# ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
# end
#
# Allow puma to be restarted by `rails restart` command.
#plugin :tmp_restart
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
I want to start using systemd since its preferred for ubuntu 16.04
I Managed To start Puma but
hi i managed to make puma server run. but now i am getting nginx upstream connection timed out.
here is out put from systemctl status puma
/system.slice/puma.service
├─ 1258 puma: cluster worker 0: 15954 [mystore2.rentcallcenter.com]
└─15954 puma 3.10.0 (unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock)
here is my nginx server block file
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
server_name mystore2.rentcallcenter.com;
root /var/www/mystore2.rentcallcenter.com/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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}`

i managed to connect to make my rails app run with puma and nginx
final config
nginx server block file
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///var/www/mystore2.rentcallcenter.com/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
listen [::]:80;
server_name mystore2.rentcallcenter.com;
root /var/www/mystore2.rentcallcenter.com/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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
and puma.service
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
# Foreground process (do not use --daemon in ExecStart or config.rb)
Type=simple
# Preferably configure a non-privileged user
User=www-data
# The path to the puma application root
# Also replace the "<WD>" place holders below with this path.
WorkingDirectory=/var/www/mystore2.rentcallcenter.com
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
# The command to start Puma. This variant uses a binstub generated via
# `bundle binstubs puma --path ./sbin` in the WorkingDirectory
# (replace "<WD>" below)
# ExecStart=/home/ashish/.rbenv/shims/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
# Variant: Use config file with `bind` directives instead:
ExecStart= /home/ashish/.rbenv/shims/puma -C /var/www/mystore2.rentcallcenter.com/config/puma.rb
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
Restart=always
[Install]
WantedBy=multi-user.target

Related

Nginx and Unicorn not working due to unicorn.sock file error

I am setting up VPS for the first time on upcloud. I am using unicorn 5.5.5 with Nginx. Rails 4.2.8 and ruby 2.4.2. My Nginx service runs fine. Doesn't show any error. Whenever I start unicorn service I get this error. I followed this tutorial.
https://medium.com/#manishyadavv/how-to-deploy-ruby-on-rails-apps-on-aws-ec2-7ce55bb955fa
F, [2020-06-16T12:44:20.611895 #12683] FATAL -- : error adding listener addr=/tmp/unicorn.sock
/usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/socket_helper.rb:132:in `bind_listen': socket=/tmp/unicorn.sock specified but it is not a socket! (ArgumentError)
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/http_server.rb:243:in `listen'
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/http_server.rb:851:in `block in bind_new_listeners!'
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/http_server.rb:851:in `each'
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/http_server.rb:851:in `bind_new_listeners!'
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/lib/unicorn/http_server.rb:142:in `start'
from /usr/share/rvm/gems/ruby-2.4.2/gems/unicorn-5.5.5/bin/unicorn:128:in `<top (required)>'
from /usr/share/rvm/gems/ruby-2.4.2/bin/unicorn:23:in `load'
from /usr/share/rvm/gems/ruby-2.4.2/bin/unicorn:23:in `<main>'
from /usr/share/rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:24:in `eval'
from /usr/share/rvm/gems/ruby-2.4.2/bin/ruby_executable_hooks:24:in `<main>'
My unicorn.sock file is empty, I am sorry if this is a rookie mistake but I am stuck with this issue for 2 days now. Kindly help me out.
Here is my unicorn_repo file in the etc/init.d directory
#!/bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn app server
# Description: starts unicorn using start-stop-daemon
### END INIT INFO
set -e
USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"
# app settings
USER="root"
APP_NAME="soup"
APP_ROOT="/$USER/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"
here is my nginx/default file
upstream app {
# Path to Unicorn SOCK file, as defined previously
server unix:/tmp/unicorn.sock fail_timeout=0;
}
server {
listen 0.0.0.0;
server_name localhost;
root root/soup/;
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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Here is my unicorn.rb file.
# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir
# Set unicorn options
worker_processes 2
preload_app true
timeout 30
# Set up socket location
listen "/tmp/unicorn.sock", :backlog => 64
listen 3000, :tcp_nopush => true
# Logging
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"
~
~
~
~
A socket is a special kind of file used for inter process communication.
If you you run ls -la sockets have a leading s in the mode string.
Yours should look something like this:
srwxrwxrwx /tmp/unicorn.sock
If you have created /tmp/unicorn.sock as a file manually, delete it.
As a side note: The tutorial you are using is 3 years old and so your setup is outdated from the start.

Nginx 502 when running Rails (Puma) with -d (daemon)

Ruby 2.5.1, Rails 5.2.2.1
I'm trying to make nginx get upstream through puma socket.
When I run rails s -e production all is good.
When I run rails s -e production -d Nginx returns 502 Bad Gateway
config/puma.rb
...
app_dir = "/home/user/myapp"
tmp_dir = "#{app_dir}/tmp"
# Set up socket location
bind "unix://#{tmp_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{app_dir}/log/puma.stdout.log", "#{app_dir}/log/puma.stderr.log", true
...
etc/nginx/sites-enabled/mydomain.com
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:/home/user/myapp/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name mydomain.com;
root /home/user/myapp/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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
var/log/nginx/error.log
2019/07/07 13:45:09 [error] 21609#21609: *11391 connect() to
unix:/home/user/myapp/tmp/sockets/puma.sock failed (111: Connection
refused) while connecting to upstream, client: 172.68.11.91, server:
mydomain.com, request: "GET /pages/one HTTP/1.1", upstream:
"http://unix:/home/user/myapp/tmp/sockets/puma.sock:/pages/one", host: "mydomain.com"
(P.S. change from original domain to mydomain.com)
What difference? How to fix it? Please explain and help
UPDATE
Seems to be running with daemon flag it doesn't create puma.sock in /home/user/myapp/tmp/sockets. Why and where is it?
The earlier answer does not work any more. The deamon option -d is deprecated.
You could use a systemd service:
sudo nano /etc/systemd/system/puma.service
Copy this to the file and fill in your YOUR_APP_PATH and FULLPATH:
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
# Puma supports systemd's `Type=notify` and watchdog service
# monitoring, if the [sd_notify](https://github.com/agis/ruby-sdnotify) gem is installed,
# as of Puma 5.1 or later.
# On earlier versions of Puma or JRuby, change this to `Type=simple` and remove
# the `WatchdogSec` line.
Type=notify
# If your Puma process locks up, systemd's watchdog will restart it within seconds.
WatchdogSec=10
# Preferably configure a non-privileged user
# User=
# The path to your application code root directory.
# Also replace the "<YOUR_APP_PATH>" placeholders below with this path.
# Example /home/username/myapp
WorkingDirectory=<YOUR_APP_PATH>
# Helpful for debugging socket activation, etc.
# Environment=PUMA_DEBUG=1
# SystemD will not run puma even if it is in your path. You must specify
# an absolute URL to puma. For example /usr/local/bin/puma
# Alternatively, create a binstub with `bundle binstubs puma --path ./sbin` in the WorkingDirectory
ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/puma.rb
# Variant: Rails start.
# ExecStart=/<FULLPATH>/bin/puma -C <YOUR_APP_PATH>/config/puma.rb ../config.ru
# Variant: Use `bundle exec --keep-file-descriptors puma` instead of binstub
# Variant: Specify directives inline.
# ExecStart=/<FULLPATH>/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
Restart=always
[Install]
WantedBy=multi-user.target
Run systemctl daemon-reload to reload your services.
Then you can use sudo systemctl restart puma to restart/start/stop the service
Refer to the puma docs for more information.
Solution
Dont know why, but It works if run puma (not rails server)
RAILS_ENV=production bundle exec puma -C config/puma.rb -d

rails 5 with puma with nginx systemd

right now i have a droplet in DO and experimenting in rails deployment procedures. I have a rails 5 in ubuntu 16 and have done all necessary procedures to be able to deploy my app except that currently stuck in nginx puma and systemd. I've already seen DO's tutorials but the problem is that they use upstart as the script for init. As a complete beginner in nginx and linux systems, i have no idea how to start my app with nginx. the last procedures i made was
install nginx
test nginx, it is running
put some configs on config/puma.rb*
# Change to match your CPU core count
workers 2
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("# {app_dir}/config/database.yml")[rails_env])
end
edited /etc/nginx/sites-available/default*
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:/home/deploy/appname/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /home/deploy/appname/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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
added /etc/systemd/system/puma.service
*base from https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04
this is the code of puma.service
[Unit]
Description=Puma HTTP Server
After=network.target
# Uncomment for socket activation (see below)
# Requires=puma.socket
[Service]
Type=simple
User=(myUser)
WorkingDirectory=/home/(myUser)/apps/(appName)
# ExecStart=<WD>/sbin/puma -b tcp://0.0.0.0:9292 -b ssl://0.0.0.0:9293?key=key.pem&cert=cert.pem
# ExecStart=/usr/local/bin/bundle exec --keep-file-descriptors puma -e production
ExecStart=/usr/local/bin/puma -C /home/(myUser)/apps/(appName)/config/puma.rb
Restart=always
[Install]
WantedBy=multi-user.target
im having exit 203, check some things and found out /usr/local/bin/puma doesnt exist in my linux.
Can someone help my setting up a proper systemd service? i really have no experience in this and im not sure what to put at ExecStart
Install puma if it is not already.
Use which puma to find out where puma is installed, then put that path on ExecStart= line.

Nginx can't connect to localhost

i found myself following this tutorial "How To Deploy a Rails App with Puma and Nginx on Ubuntu 14.04" (https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04) in order to use ngnix as load balancer.
i followed all the steps and works fine, i suppose, but at the end, when i configured the nginx config file, i can't access into the localhost (0.0.0.0:3000) it shows "Unable to connect". i checked if the process is running and yes...
Here i let some files
/etc/nginx/sites-available/iaw2015.conf
upstream app {
# Path to Puma SOCK file, as defined previously
server unix:///var/www/iaw2015/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /var/www/iaw2015/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 Host $http_host;
proxy_redirect off;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
i ran this command sudo ln -sf /etc/nginx/sites-available/iaw2015.conf /etc/nginx/sites-enabled/iaw2015.conf to create the link.
config/puma.rb (in my project)
# Change to match your CPU core count
workers 2
# Min and Max threads per worker
threads 1, 6
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
Based on what you posted, nothing is running on port 3000 that you are trying to connect to. Instead, an app server is running on a socket located at:
/var/www/iaw2015/shared/sockets/puma.sock
First, check that the app is running that you can communicate with it through the socket.
Next, try connecting to http://127.0.0.1/ This will connect to port 80, where Nginx is running. It should hopefully proxy to your app and return a result.

Using unicorn and nginx for ruby 2.0.0 based rails application

I have been trying to configure unicorn and nginx to run my ruby 2.0.0 on rails 3.2.13 to run e-commerce application. I have been trying lot of the examples and blog and trying it out, but I am not to configure the unicorn and nginx. I am pasting nginx and unicorn files here. Kindly have a look into it. Initially I used to get 500 error now I am getting 400 bad request error.
unicorn.rb
root = "/home/ubuntu/apps/new_spree_st"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn_stderr.log"
stdout_path "#{root}/log/unicorn_stdout.log"
listen "/tmp/unicorn.new_spree_st.sock"
worker_processes 4
timeout 30
# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end
nginx.conf
# This is example contains the bare mininum to get nginx going with Unicorn or Rainbows! servers. Generally these configuration settings are
# applicable to other HTTP application servers (and not just Ruby ones), so if you have one working well for proxying another app server, feel
# free to continue using it.
#
# The only setting we feel strongly about is the fail_timeout=0
# directive in the "upstream" block. max_fails=0 also has the same
# effect as fail_timeout=0 for current versions of nginx and may be
# used in its place.
#
# Users are strongly encouraged to refer to nginx documentation for more
# details and search for other example configs.
# you generally only need one nginx worker unless you're serving
# large amounts of static files which require blocking disk reads
#worker_connections 4096;
worker_processes 4;
# # drop privileges, root is needed on most systems for binding to port 80
# # (or anything < 1024). Capability-based security may be available for
# # your system and worth checking out so you won't need to be root to
# # start nginx to bind on 80
#user nobody nogroup; # for systems with a "nogroup"
# user nobody nobody; # for systems with "nobody" as a group instead
# Feel free to change all paths to suite your needs here, of course
pid /etc/nginx/nginx.pid;
error_log /var/log/nginx/nginx.error.log;
events {
worker_connections 4024; # increase if you have lots of clients
accept_mutex off; # "on" if nginx worker_processes > 1
# use epoll; # enable for Linux 2.6+
# use kqueue; # enable for FreeBSD, OSX
}
http {
# nginx will find this file in the config directory set at nginx build time
include mime.types;
# fallback in case we can't determine a type
default_type application/octet-stream;
# click tracking!
access_log /var/log/nginx/nginx.access.log combined;
# you generally want to serve static files with nginx since neither
# Unicorn nor Rainbows! is optimized for it at the moment
sendfile on;
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
# we haven't checked to see if Rack::Deflate on the app server is
# faster or not than doing compression via nginx. It's easier
# to configure it all in one place here for static files and also
# to disable gzip for clients who don't get gzip/deflate right.
# There are other gzip settings that may be needed used to deal with
# bad clients out there, see http://wiki.nginx.org/NginxHttpGzipModule
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/xml text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
# this can be any application server, not just Unicorn/Rainbows!
upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
# for UNIX domain socket setups:
server 127.0.0.1:80 fail_timeout=0;
# for TCP setups, point these to your backend servers
# server 192.168.0.7:8080 fail_timeout=0;
# server 192.168.0.8:8080 fail_timeout=0;
# server 192.168.0.9:8080 fail_timeout=0;
}
server {
# enable one of the following if you're on Linux or FreeBSD
# listen 80 default deferred; # for Linux
# listen 80 default accept_filter=httpready; # for FreeBSD
# If you have IPv6, you'll likely want to have two separate listeners.
# One on IPv4 only (the default), and another on IPv6 only instead
# of a single dual-stack listener. A dual-stack listener will make
# for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
# instead of just "10.0.0.1") and potentially trigger bugs in
# some software.
# listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
client_max_body_size 4G;
server_name _;
# ~2 seconds is often enough for most folks to parse HTML/CSS and
# retrieve needed images/icons/frames, connections are cheap in
# nginx so increasing this is generally safe...
keepalive_timeout 5;
# path for static files
root /home/ubuntu/apps/new_spree_st/public;
# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
#
# try_files directive appeared in in nginx 0.7.27 and has stabilized
# over time. Older versions of nginx (e.g. 0.6.x) requires
# "if (!-f $request_filename)" which was less efficient:
# http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
try_files $uri/index.html $uri.html $uri #app;
location #app {
# an HTTP header important enough to have its own Wikipedia entry:
# http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# enable this if you forward HTTPS traffic to unicorn,
# this helps Rack set the proper URL scheme for doing redirects:
# proxy_set_header X-Forwarded-Proto $scheme;
# pass the Host: header from the client right along so redirects
# can be set properly within the Rack application
proxy_set_header Host $http_host;
# we don't want nginx trying to do something clever with
# redirects, we set the Host: header above already.
proxy_redirect off;
# set "proxy_buffering off" *only* for Rainbows! when doing
# Comet/long-poll/streaming. It's also safe to set if you're using
# only serving fast clients with Unicorn + nginx, but not slow
# clients. You normally want nginx to buffer responses to slow
# clients, even with Rails 3.1 streaming because otherwise a slow
# client can become a bottleneck of Unicorn.
#
# The Rack application may also set "X-Accel-Buffering (yes|no)"
# in the response headers do disable/enable buffering on a
# per-response basis.
# proxy_buffering off;
proxy_pass http://app_server;
}
# Rails error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/ubuntu/apps/new_spree_st/public;
}
}
}
Your unicorn process is listening on a socket, however your app_server directive for Nginx is directing traffic to TCP port 80 (which assuming Nginx is running on port 80, is just redirecting traffic back to itself in an endless loop).
See the sample unicorn nginx config file. Similar to that, your app_server should look like this:
upstream app_server {
server unix:/tmp/unicorn.new_spree_st.sock fail_timeout=0;
}

Resources