如何在ubuntu上设置unicorn和nginx

时间:2014-07-10 06:50:06

标签: ruby-on-rails-3 nginx unicorn

Ubuntu 12.04,nginx 1.6, Rails 3.2.12, rbenv

我刚刚将unicorn添加到我的gemfile

然后添加了unicorn.rb并添加了修改nginx.conf

Nothig我做了。

并出现以下错误。

Starting nginx: 
nginx: [emerg] "upstream" directive is not allowed here in /opt/nginx/conf/nginx.conf:1

nginx.conf

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

我是否错过了一些必要的步骤?

unicorn.rb

#refer https://www.digitalocean.com/community/tutorials/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5
#https://devcenter.heroku.com/articles/rails-unicorn
# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/home/poc/projects/zeus"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/home/poc/projects/zeus/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/home/poc/projects/zeus/log/unicorn_err.log"
stdout_path "/home/poc/projects/zeus/log/unicorn_std.log"

# Unicorn socket
listen "/tmp/unicorn.zeus.sock"

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true

before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end

  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

nginx.conf

upstream app {
    server unix:/tmp/unicorn.zeus.sock fail_timeout=0;
}
#user git root;
#user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

    root /home/poc/projects/zeus/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;


    }       


}

1 个答案:

答案 0 :(得分:0)

嗯,它明确地说"upstream" directive is not allowed here。如果你查看upstream的文档,你会看到,该指令必须在http块内。

所以配置应如下所示:

http {
    .. stuff ..

    upstream app {
        ...
    }

    server {
        ...
    }
}