使用 nginx + unicorn 的多个 Rails 4 应用程序 [英] Multiple Rails 4 app using nginx + unicorn

查看:40
本文介绍了使用 nginx + unicorn 的多个 Rails 4 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用 unicorn 设置 nginx 服务器.我设置了第一个应用程序,但它位于根目录/"上.我真正想要的是输入 localhost/app1 并且它会运行,而如果只是进入根目录,html 或 php 页面将被打开.

I'm looking for set up a nginx server with unicorn. I the first app is set but it's on the root "/". what i really want is type localhost/app1 and it would run, while if a just enter to the root, html or php pages are going to be open.

有什么线索吗?

这是当前的 nginx.config:

Here's the current 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;
    }
  }
}

推荐答案

我知道了!事实证明这真的很简单,我在我的博客上写了一篇关于它的文章.http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/

I've got it! Turns out it was really simple and I wrote a post about it on my blog. http://jrochelly.com/post/2013/08/nginx-unicorn-multiple-rails-apps/

内容如下:

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

I'm using Ruby 2.0 and Rails 4.0. I suppose you already have nginx and unicorn installed. So, let's get started!

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

In you nginx.conf file we are going to make nginx point to a unicorn socket:

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

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

Then, with your server listening to port 80, add a location block that points to the subdirectory your rails app is (this code, must be inside server block):

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

现在您可以将独角兽作为守护者:

Now you can just Unicorn as a Deamon:

sudo unicorn_rails -c config/unicorn.rb -D

最后一件事,也是我挖得最多的一件事是为您的 rails 路由文件添加一个范围,如下所示:

The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this:

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

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

这样,您的应用程序将映射一个链接/myapp/welcome,而不只是/welcome

This way, your app will map a link /myapp/welcome, intead of just /welcome

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

Well, the above will work on production server, but what about development? Are you going to develop normally then on deployment you change your rails config? For every single app? That's not needed.

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

So, you need to create a new module that we are going to put at lib/route_scoper.rb:

require 'rails/application'

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

之后,在您的 routes.rb 中执行以下操作:

After that, in your routes.rb do this:

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 上的根目录:

What we are doing is to see if the root directory is specified, if so use it, otherwise, got to "/". Now we just need to point the root directory on 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 根.

In config/enviroments/development.rb I do not specify the config.root_directory. This way it uses the normal url root.

这篇关于使用 nginx + unicorn 的多个 Rails 4 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆