nginx error: (13: Permission denied) while connecting to upstream) - ruby-on-rails

I am running rails app with puma, capistrano, and nginx on a google compute engine VM with ubuntu 14.04 LTS.
I have the nginx running on the external IP. And when I visit it I get two nginx errors in the log:
2016/02/03 11:58:07 [info] 19754#0: *73 client closed connection while waiting for request, client: ###.##.##.###, server: 0.0.0.0:443
2016/02/03 11:58:07 [crit] 19754#0: *74 connect() to unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock failed (13: Permission denied) while connecting to upstream, client: ###.##.##.###, server: ,
request: "GET / HTTP/1.1", upstream: "http://unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock:/", host: "###.###.###.###"
Note: the last ###.###.###.### is the external IP of the google compute VM that the code is running on. I believe the 1st two IP's are my home IP.
I have tried: setsebool httpd_can_network_connect on as suggested here:
(13: Permission denied) while connecting to upstream:[nginx]
And it returned: setsebool: SELinux is disabled. But the problem persists.
I have looked at (13: Permission denied) while connecting to upstream:[nginx] as well, but it seems to be particular to uwsgi
Here is my nginx.conf
upstream puma {
server unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock;
}
server {
listen 80 default_server deferred;
listen 443 ssl;
# server_name example.com;
ssl_certificate /etc/ssl/my-web-app/my-web-app.com.chained.crt;
ssl_certificate_key /etc/ssl/my-web-app/my-web-app.key;
root /home/my-web-app/apps/my-web-app/current/public;
access_log /home/my-user-name/apps/my-web-app/current/log/nginx.access.log;
error_log /home/my-user-name/apps/my-web-app/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 10M;
keepalive_timeout 10;
}
I run nginx with sudo service nginx restart
Then I run puma with: RACK_ENV=production bundle exec puma -p 3000 and it returns:
Puma starting in single mode...
* Version 2.14.0 (ruby 2.1.7-p400), codename: Fuchsia Friday
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
EDIT 1
It was suggested that I run puma on unix not tcp 3000 so that it'd match nginx
I have tried running puma on unix via the command:
RACK_ENV=production bundle exec puma -d -b unix:///tmp/my-web-app.sock --pidfile /tmp/puma.pid
which gave:
Puma starting in single mode...
* Version 2.14.0 (ruby 2.1.7-p400), codename: Fuchsia Friday
* Min threads: 0, max threads: 16
* Environment: production
* Daemonizing...
It reads the above text, but it does not linger, the command prompt occurs again immediately despite the '...' at the end.
This command seemingly does not work, so if anyone can suggest how to run puma on unix and not tcp 3000, then I could complete the suggestion. (Though I suspect there is a configuring nginx issue that may be occuring before anything that has to do with puma)
EDIT 2 Attaching puma.rb
#!/usr/bin/env puma
directory '/home/my-user-name/apps/my-web-app/current'
rackup "/home/my-user-name/apps/my-web-app/current/config.ru"
environment 'production'
pidfile "/home/my-user-name/apps/my-web-app/shared/tmp/pids/puma.pid"
state_path "/home/my-user-name/apps/my-web-app/shared/tmp/pids/puma.state"
stdout_redirect '/home/my-user-name/apps/my-web-app/current/log/puma.error.log', '/home/my-user-name/apps/my-web-app/current/log/puma.access.log', true
threads 2,8
bind 'unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock'
workers 1
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = "/home/my-user-name/apps/my-web-app/current/Gemfile"
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
EDIT 3
I now tried just running the rails server on port 80 directly. I typed:
rvmsudo rails server -p 80 and it returned:
=> Booting Puma
=> Rails 4.2.4 application starting in development on http://localhost:80
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:80
Exiting
/home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `initialize': Address already in use - bind(2) for "localhost" port 80 (Errno::EADDRINUSE)
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `new'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `add_tcp_listener'
from (eval):2:in `add_tcp_listener'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/rack/handler/puma.rb:33:in `run'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/server.rb:80:in `start'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
EDIT 4
If I run sudo service nginx stop then run rvmsudo rails server -p 80 again it returns:
=> Booting Puma
=> Rails 4.2.4 application starting in development on http://localhost:80
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:80
That means that approach was incorrect since without nginx when I visit the external IP nothing its now returns The server refused the connection. as opposed to the original:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
If anyone knows how to prevent the original error, any suggestions would be much appreciated.
EDIT 5
The original question remains, but can anyone tell me if this is an https problem or an ssl problem?
EDIT 6
I have tried running puma directly on 80 and am getting a permission error on 80.
I try: RACK_ENV=production bundle exec puma -p 80 and get:
Puma starting in single mode...
* Version 2.14.0 (ruby 2.1.7-p400), codename: Fuchsia Friday
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://0.0.0.0:80
/home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `initialize': Permission denied - bind(2) for "0.0.0.0" port 80 (Errno::EACCES)
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `new'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `add_tcp_listener'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:98:in `block in parse'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:84:in `each'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:84:in `parse'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/runner.rb:119:in `load_and_bind'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/single.rb:79:in `run'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/cli.rb:215:in `run'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/bin/puma:10:in `<top (required)>'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/bin/puma:23:in `load'
from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/bin/puma:23:in `<main>'
I believe that this is caused because port 80 has higher permissions than others. So, I ran sudo RACK_ENV=production bundle exec puma -p 80 but that just returned: Your Ruby version is 1.9.3, but your Gemfile specified 2.1.7

I got the same error with you, I got a solution but don't know should it be right.
Change the first line of file /etc/nginx/nginx.conf
user www-data;
to
user root;
Then restart the nginx using:
service nginx restart OR systemctl restart nginx
WARNING: This runs your web server as the root user. This should never be done in a production environment as it allows the web server processes full access to your system. If the web server process is compromised, the attacker will have unrestricted access to your whole server.

Take a look at your puma.error.log file for your application.
Mine was complaining about a small syntax error in a config block on production environment.

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

Starting a rails app to the external ip address

I have a ruby on rails application and I am trying to run it on the external ip of my google compute engine ubuntu 14.04 LTS VM.
I try rails server -e production
and the output is:
=> Booting Puma
=> Rails 4.2.4 application starting in production on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://localhost:3000
I do not want it to be at that location; I want it to be viewable from the external ip address of the server.
Part of the issue is that I do not know if this is a rails, a puma, or a google compute engine question.
Note: I can't see if it actually launching at localhost:3000 because the VM is just a terminal.
(I am assuming you are using nginx, if not, apt-get install nginx; service nginx start)
If possible, show your nginx.conf (/etc/nginx/nginx.conf) and default.conf (/etc/nginx/sites-available/default.conf)
Since you are using puma (I use it too) you should setup nginx conf file and set server upstream equal to puma's binding.
# /etc/nginx/sites-available/default.conf
upstream your_app {
server 127.0.0.0.1:9292;
}
server {
listen 80;
server_name your_domain;
root /usr/share/nginx/your_app/public;
# or, if you are using capistrano
root /usr/share/nginx/your_app/current/public;
location / {
proxy_pass http://your_app; # equal to upstream "name"
...
}
....
}
# config/puma.rb
[...]
bind "tcp://127.0.0.1:9292"
And execute puma's server
$ bundle exec puma RAILS_ENV=production &
After doing this steps and if the application still doesn't work, output your /var/log/nginx/error.log, nginx.conf and default.conf

Nginx configuration 502 bad gateway error

I have a configured nginx sites enabled/available that when I run:
nginx -t
I get a successful test and when I:
service nginx restart
thin restart -s 1
My 502 error goes away but only for ten seconds and then it shows up again. my sites-available is:
upstream myapp {
server 0.0.0.0:3000;
}
server {
listen 80 default;
access_log /webservices/crawler/log/access.log;
error_log /webservices/crawler/error.log;
root /webservices/crawler/public/;
index index.html;
client_max_body_size 500M;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html;
break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html;
break;
}
if (!-f $request_filename) {
proxy_pass http://myapp;
break;
}
}
}
Does anyone know why my config only works for a few seconds? Thank you for any help and just let me know if you need to see any of my other code.
thin restart -s 2 output:
/# thin restart -s 2
Stopping server on 0.0.0.0:3000 ...
/usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:131:in `send_signal': Can't stop process, no PID found in tmp/pids/thin.3000.pid (Thin::PidFileNotFound)
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:113:in `kill'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:93:in `block in stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:134:in `tail_log'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:92:in `stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:187:in `run_command'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:152:in `run!'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/bin/thin:6:in `<top (required)>'
from /usr/local/bin/thin:23:in `load'
from /usr/local/bin/thin:23:in `<main>'
Stopping server on 0.0.0.0:3001 ...
/usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:131:in `send_signal': Can't stop process, no PID found in tmp/pids/thin.3001.pid (Thin::PidFileNotFound)
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:113:in `kill'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:93:in `block in stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:134:in `tail_log'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:92:in `stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:187:in `run_command'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:152:in `run!'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/bin/thin:6:in `<top (required)>'
from /usr/local/bin/thin:23:in `load'
from /usr/local/bin/thin:23:in `<main>'
Starting server on 0.0.0.0:3000 ...
Starting server on 0.0.0.0:3001 ...
I am pretty sure this is a Thin issue and not an nginx issue so I am posting an update. Sorry for any confusion.
my website is up for about 10 seconds and then I get the 502 nginx error. My nginx config seems fine and the nginx -t is successful. My error message is this:
thin restart -s 5
/usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:131:in send_signal': Can't stop process, no PID found in tmp/pids/thin.3004.pid (Thin::PidFileNotFound)
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/daemonizing.rb:113:inkill'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:93:in block in stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:134:intail_log'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/controllers/controller.rb:92:in stop'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:187:inrun_command'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/lib/thin/runner.rb:152:in run!'
from /usr/local/lib/ruby/gems/2.0.0/gems/thin-1.5.1/bin/thin:6:in'
from /usr/local/bin/thin:23:in load'
from /usr/local/bin/thin:23:in'
I have checked the PID,s and they are there. Does anyone have any idea on what is going on here? thank you and I appreciate any advice.
I found what I needed to run to clean up those thin errors. I don't know why but running:
bundle exec thin start -p 3000
fixed my situation. Thanks for everyone's help! I could not have found it without you.

NGinx sock connect Bad Gateway

I'm having an issue setting up Nginx to work with Puma server for a Rails 4 application.
The problem seems to be in my Nginx configuration since I keep getting 502 Bad Gateway error and error log states the following:
*1 connect() to unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock failed
(2: No such file or directory) while connecting to upstream, client: XX.XXX.XX.XXX,
server: mysitename.com, request: "GET /favicon.ico HTTP/1.1", upstream:
"http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:/favicon.ico",
host: "mysitename.com"
Here is the Nginx site configuration I'm using:
upstream mysitename {
server unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock;
}
server {
listen 80;
server_name mysitename.com;
root /srv/vhosts/rumysitename/www/mysitename/public;
location / {
proxy_pass http://mysitename;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
}
Needless to say that Puma can't connect to that sock because it isn't there:
rails s -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock=> Booting Puma
=> Rails 4.1.0 application starting in production on http://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.8.2 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock:3000
Exiting
/srv/vhosts/rumysitename/.rvm/gems/ruby-2.1.0/gems/puma-2.8.2/lib/puma/binder.rb:195:in `initialize': getaddrinfo: Name or service not known (SocketError)
I am no system admin and have absolutely no experience with Nginx so excuse me if I'm missing something obvious.
It seems that starting rails server and asking it to bind to a unix socket doesn't work. The -b option with rails server doesn't behave the same was as with the puma command. Basically it wants to bind to an IP address:
rails server --help
Usage: rails server [mongrel, thin, etc] [options]
-p, --port=port Runs Rails on the specified port.
Default: 3000
-b, --binding=ip Binds Rails to the specified ip.
Default: 0.0.0.0
You can run puma directly:
puma -e production -b unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock
The socket will be created by running puma like this. It doesn't need to exist already. You'll need to have permission to create it in the location specified but you'll get a different error if that is not the case.
Another alternative is to create a config/puma.rb file which can include the binding:
config/puma.rb:
bind 'unix:///srv/vhosts/rumysitename/www/mysitename/tmp/mysitename.sock'
and then run puma referencing that:
puma -C config/puma.rb -e production
You can put a lot more in the config file than just the sock. The puma example config file is a good starting point.
Try following steps.
Stop the puma server.
Delete the .sock file used by puma.
Start the puma server.

Resources