【问题标题】:Running unicorn server as daemon (reverse proxy to nginx)将独角兽服务器作为守护进程运行(反向代理到 nginx)
【发布时间】:2015-11-08 06:28:17
【问题描述】:

我正在运行带有独角兽的 Rails 服务器。这是我的配置。

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;
    }
}

使用rails server unicorn 一切正常,但是当我尝试使用rails server unicorn -d 使其成为守护进程时,出现连接错误。

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"

可能出了什么问题?

【问题讨论】:

    标签: ruby-on-rails nginx unicorn


    【解决方案1】:

    命令应该是unicorn -c config/unicorn.rb -D,并在app目录下执行命令。

    来自https://www.digitalocean.com/community/tutorials/how-to-deploy-sinatra-based-ruby-web-applications-on-ubuntu-13 的提示。

    【讨论】:

      【解决方案2】:

      unicorn-rails 存在问题。见https://github.com/samuelkadolph/unicorn-rails/issues/25

      当您将-d (--daemon) 命令行标志传递给rails server 时,Rack 会使用Process.daemon 来守护进程,这会执行隐式chroot("/")。之后,unicorn-rails 尝试在相对文件路径下找到config/unicorn.rb,当然文件系统根目录中缺少该路径(/config/unicorn.rb 不存在)。

      这就是为什么在您的情况下,/tmp/unicorn.sock 未配置并且反向代理失败。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-27
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-02
        • 2016-09-07
        • 1970-01-01
        • 2011-08-08
        相关资源
        最近更新 更多