使用nginx +独角兽的多个Rails 4应用程序

时间:2013-08-08 19:04:02

标签: ruby-on-rails nginx unicorn

我正在寻找使用独角兽设置nginx服务器。我设置了第一个应用程序,但它位于根“/”上。我真正想要的是输入localhost / app1并且它会运行,而如果只是输入到root,html或php页面将会打开。

有任何线索吗?

这是当前的nginx.config:

worker_processes 4;

user nobody nogroup; # for systems with a "nogroup"

pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1
}

http {
  include mime.types;


  default_type application/octet-stream;
  access_log /tmp/nginx.access.log combined;

  sendfile on;

  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff

  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  upstream sip {
    server unix:/home/analista/www/sip/tmp/sockets/sip.unicorn.sock fail_timeout=0;
  }

  server {

    listen 80 default deferred; # for Linux


    client_max_body_size 4G;
    server_name sip_server;

    keepalive_timeout 5;

    # path for static files
    root /home/analista/www/sip/public;

    try_files $uri/index.html $uri.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_buffering off;

      proxy_pass http://sip;
    }

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (!-f $request_filename) {
        proxy_pass http://sip;
        break;
      }
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /home/analista/www/sip/public;
    }
  }
}

1 个答案:

答案 0 :(得分:7)

我知道了! 原来这很简单,我在博客上写了一篇关于它的帖子。 http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/

以下是内容:

我正在使用 Ruby 2.0 Rails 4.0 。我想你已经安装了nginx和unicorn。那么,让我们开始吧!

在你的 nginx.conf 文件中,我们将使nginx指向一个独角兽套接字:

upstream unicorn_socket_for_myapp {
  server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

然后,在服务器侦听端口80的情况下,添加一个指向rails应用程序子目录的位置块(此代码必须位于服务器块内):

location /myapp/ {
    try_files $uri @unicorn_proxy;
  }

  location @unicorn_proxy {
    proxy_pass http://unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

现在你可以将Unicorn作为Deamon:

sudo unicorn_rails -c config/unicorn.rb -D

最后要做的事情,我挖出的最多的是为rails路由文件添加一个范围,如下所示:

MyApp::Application.routes.draw do
  scope '/myapp' do
    root :to => 'welcome#home'

    # other routes are always inside this block
    # ...
  end
end

这样,您的应用就会映射一个链接 / myapp / welcome ,只需 / welcome

但是还有更好的方法

嗯,以上内容适用于生产服务器,但是开发呢?您是否要正常开发然后在部署时更改rails配置?每个应用程序?这不是必需的。

因此,您需要创建一个我们将放在lib/route_scoper.rb的新模块:

require 'rails/application'

module RouteScoper
  def self.root
    Rails.application.config.root_directory
  rescue NameError
    '/'
  end
end

之后,在routes.rb执行此操作:

require_relative '../lib/route_scoper'

MyApp::Application.routes.draw do
  scope RouteScoper.root do
    root :to => 'welcome#home'

    # other routes are always inside this block
    # ...
  end
end

我们正在做的是查看是否指定了根目录,如果是,则使用它,否则,得到“/”。现在我们只需要在config / enviroments / production.rb上指向根目录:

MyApp::Application.configure do
  # Contains configurations for the production environment
  # ...

  # Serve the application at /myapp
  config.root_directory = '/myapp'
end

在config / enviroments / development.rb中,我没有指定config.root_directory。这样它就会使用普通的url root。