简体   繁体   中英

Running unicorn server as daemon (reverse proxy to nginx)

I'm running a rails server with unicorn. This is my configuration.

config/unicorn.rb

# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
working_directory app_dir

# Set unicorn options
worker_processes 1
preload_app true
timeout 30

# Set up socket location
#listen "#{shared_dir}/sockets/unicorn.sock", :backlog => 64
listen "/tmp/unicorn.sock", :backlog => 64

# Logging
stderr_path "/var/log/unicorn/stderr.log"
stdout_path "/var/log/unicorn/stdout.log"

# Set master PID location
pid "/tmp/unicorn.pid"

/etc/nginx/site-enabled/rails_unicorn.conf

upstream app {
    server unix:/tmp/unicorn.sock fail_timeout=0;
    keepalive 8;
}

server {
    listen 80;
    server_name rails.example.com;
    passenger_enabled on;
    passenger_app_env development;
    root /home/ubuntu/webapp/rails/simple/public;

    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_set_header X-NginX-Proxy true;

        proxy_pass http://app/;
        proxy_redirect off;
    }
}

Everything works fine with rails server unicorn , but when I tried to make it a daemon with rails server unicorn -d , I have connection error.

2015/11/08 00:21:06 [error] 9587#0: *5 connect() to unix:/tmp/unicorn.sock
failed (111: Connection refused) while connecting to upstream, client: 
68.203.30.28, server: rails.example.com, request: "GET / HTTP/1.1", upstream: 
"http://unix:/tmp/unicorn.sock:/", host: "rails.example.com"

What might be wrong?

The command should be unicorn -c config/unicorn.rb -D and execute the command in the app directory.

Hints from https://www.digitalocean.com/community/tutorials/how-to-deploy-sinatra-based-ruby-web-applications-on-ubuntu-13 .

There is an issue with unicorn-rails . See https://github.com/samuelkadolph/unicorn-rails/issues/25

When you pass the -d ( --daemon ) command line flag to rails server , then Rack daemonizes the process using Process.daemon , which does an implicit chroot("/") . After that, unicorn-rails tries to find config/unicorn.rb under a relative file path, which of course is missing from the filesystem root directory ( /config/unicorn.rb doesn't exist).

That is why in your case, /tmp/unicorn.sock is not configured and the reverse proxy fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM