独角兽+ Nginx +基于Sinatra的VPS与主工作者失败

时间:2014-07-09 21:54:38

标签: ruby nginx sinatra unicorn digital-ocean

我正在尝试使用Unicorn将Sinatra应用程序部署到我的服务器上。但是,当我尝试运行unicorn -c path/to/unicorn.rb -E development -D时出现master failed to start, check stderr log for details错误。这是stderr日志文件:

FATAL -- : error adding listener addr=../rails/tmp/sockets/unicorn.sock /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/socket_helper.rb:167:in `bind_listen': Don't know how to bind: ../rails/tmp/sockets/unicorn.sock (Argum$
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:255:in `listen'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:801:in `block in bind_new_listeners!'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:801:in `each'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:801:in `bind_new_listeners!'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:146:in `start'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'

另外,我在我的应用程序文件夹中创建了unicorn tmp,log,tmp / pid,tmp / sockets文件夹,并创建了unicorn.conf和unicorn.rb,如下所示:

unicorn.rb

 # set path to app that will be used to configure unicorn,
# note the trailing slash in this example
@dir = "./"

worker_processes 2
working_directory @dir

timeout 30

# Specify path to socket unicorn listens to,
# we will use this in our nginx.conf later
listen "#{@dir}tmp/sockets/unicorn.sock", :backlog => 64

# Set process id path
pid "#{@dir}tmp/pids/unicorn.pid"

# Set log file paths
stderr_path "#{@dir}log/unicorn.stderr.log"
stdout_path "#{@dir}log/unicorn.stdout.log"

unicorn.conf

listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "./"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"

我认为问题可能基于nginx。所以我编辑了nginx.conf如下:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events { worker_connections 1024; }

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        gzip on;
        gzip_disable "msie6";
        gzip_types text/plain text/xml text/css text/comma-separated-values;
        upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        upstream unicorn_server {
                server unix:/home/rails/tmp/sockets/unicorn.sock
                fail_timeout=0;
        }
}

从日志中可以看出,问题是基于unicorn.sock文件。然而,当我搜索“不知道如何绑定”的问题时,我找不到任何解决方案。需要建议。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

根据socket_helper.rb的代码(请参阅here),unicorn.rb文件中的套接字路径格式可能存在问题。

bind_listen方法之前的评论说:

# creates a new server, socket. address may be a HOST:PORT or
# an absolute path to a UNIX socket. address can even be a Socket
# object in which case it is immediately returned
def bind_listen(address = '0.0.0.0:8080', opt = {})
  ...
end

所以我猜你必须使用套接字的绝对路径,而不是相对路径。

答案 1 :(得分:0)

@PA。 Buisson是正确的,并引导我解决方案。为完整起见,unicorn.rb可以更改为:

# set path to app that will be used to configure unicorn,
# note the trailing slash in this example
@dir = File.expand_path(File.dirname(__FILE__))

worker_processes 2
working_directory @dir

timeout 30

# Specify path to socket unicorn listens to,
# we will use this in our nginx.conf later
listen File.join(@dir, "tmp/sockets/unicorn.sock"), :backlog => 64

# Set process id path
pid File.join(@dir, "tmp/pids/unicorn.pid")

# Set log file paths
stderr_path File.join(@dir, "log/unicorn.stderr.log")
stdout_path File.join(@dir, "log/unicorn.stdout.log")