简体   繁体   English

nginx和unicorn上的多个rails应用程序

[英]multiple rails apps on nginx and unicorn

I successfully setup a rails site using the Screencast 335 deploy to a VPS tutorial. 我使用Screencast 335部署到VPS教程成功设置了rails站点。 Now I want to add another rails app on a new domain but I am confused about the steps required. 现在我想在新域上添加另一个rails应用程序,但我对所需的步骤感到困惑。

In the above setup, there are no changes to sites-available or /etc/nginx/nginx.conf. 在上面的设置中,site-available或/etc/nginx/nginx.conf没有变化。 The only configuration is in unicorn.rb, unicorn_init.sh and nginx.conf in my apps config directory. 唯一的配置是在我的apps配置目录中的unicorn.rb,unicorn_init.sh和nginx.conf中。 The nginx.conf file looks like this:- nginx.conf文件如下所示: -

upstream unicorn {
  server unix:/tmp/unicorn.my_app.sock fail_timeout=0;
}
server {
  listen 80 default deferred;
  # server_name my_app.com.au www.my_app.com.au;
  root /var/www/my_app/current/public;
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }
  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

In my Capistrano recipe I have this line 在我的Capistrano食谱中,我有这条线

sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"

Is adding a second domain merely a matter of removing default deferred after listen and un-commenting the server_name section then repeating this config file with a different upstream socket name and server name for the second app? 添加第二个域只是在监听和取消注释server_name部分之后删除默认延迟,然后使用不同的上游套接字名称和第二个应用程序的服务器名称重复此配置文件? Will that work or do I need to transfer this file to sites-available and create a symbolic link to sites-enabled? 是否可行或是否需要将此文件传输到可用的站点并创建启用站点的符号链接?

In unicorn.rb: 在unicorn.rb中:

application 1: 申请1:

root = "/var/www/application_1/current"
working_directory root
pid "#{root}/tmp/pids/unicorn-app1.pid"
listen "/tmp/unicorn.app1.sock"

application 2: 申请2:

root = "/var/www/application_2/current"
working_directory root
pid "#{root}/tmp/pids/unicorn-app2.pid"
listen "/tmp/unicorn.app2.sock"

In nginx.conf: 在nginx.conf中:

application 1: 申请1:

upstream app1_server {
  server unix:/tmp/unicorn.app1.sock fail_timeout=0;
}

server {
  listen 80;
  server_name my_app_1.com www.my_app_1.com;
  root /var/www/app1/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @app1_server;
  location @app1_server {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_pass http://app1_server;
 }

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

application 2: 申请2:

upstream app2_server {
  # point to app2 sock
  server unix:/tmp/unicorn.app2.sock fail_timeout=0;
}

server {
  listen 80;
  server_name my_app_2.com www.my_app_2.com;
  root /var/www/app2/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @app2_server;
  location @app2_server {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  proxy_pass http://app2_server;
 }

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

It is really easy to host different apps on one host with Nginx and Unicorn. 使用Nginx和Unicorn在一台主机上托管不同的应用程序非常容易。

The separation you can get by defining different names of the socket files of each application. 通过定义每个应用程序的socket文件的不同名称,可以获得分离。 Of course you should point the right current/public directories in the server section of nginx.conf . 当然,您应该在nginx.confserver部分指出正确的current/public目录。

The last touch is in the unicorn_init.sh file: on the top of it you should change APP_ROOT with the full path to current/public directory of your application. 最后一次触摸在unicorn_init.sh文件中:在它的顶部,您应该使用应用程序的current/public目录的完整路径更改APP_ROOT

If your setup is similar to the RailsCast's one, all the other things are done by capistrano . 如果你的设置类似于RailsCast的设置,那么所有其他的事情都由capistrano完成。

I answered this on a question I made myself here: 我在这里提出的问题上回答了这个问题:

Multiple Rails 4 app using nginx + unicorn 使用nginx +独角兽的多个Rails 4应用程序

But here's a answer: 但这是一个答案:

I'm using Ruby 2.0 and Rails 4.0 . 我正在使用Ruby 2.0Rails 4.0 I suppose you already have nginx and unicorn installed. 我想你已经安装了nginx和unicorn。 So, let's get started! 那么,让我们开始吧!

In you nginx.conf file we are going to make nginx point to a unicorn socket: 在你的nginx.conf文件中,我们将使nginx指向一个独角兽套接字:

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

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): 然后,在服务器侦听端口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;
  }

Now you can just Unicorn as a Deamon: 现在你可以把Unicorn当作一个Deamon:

sudo unicorn_rails -c config/unicorn.rb -D

The last thing to do, and the one I dug the most is to add a scope for your rails routes file, like this: 最后要做的事情,我挖出的最多的是为rails路由文件添加一个范围,如下所示:

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

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

This way, your app will map a link /myapp/welcome , intead of just /welcome 这样,您的应用程序将映射一个链接/ myapp / welcome ,intead / just

But there's a even better way 但还有更好的方法

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? 您是否要正常开发然后在部署时更改rails配置? For every single app? 每个应用程序? That's not needed. 这不是必需的。

So, you need to create a new module that we are going to put at lib/route_scoper.rb : 因此,您需要创建一个我们将放在lib/route_scoper.rb的新模块:

require 'rails/application'

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

After that, in your routes.rb do this: 之后,在您的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

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: 现在我们只需要在config / enviroments / production.rb上指向根目录:

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

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

In config/enviroments/development.rb I do not specify the config.root_directory. 在config / enviroments / development.rb中,我没有指定config.root_directory。 This way it uses the normal url root. 这样它就会使用普通的url root。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM