簡體   English   中英

nginx和unicorn上的多個rails應用程序

[英]multiple rails apps on nginx and unicorn

我使用Screencast 335部署到VPS教程成功設置了rails站點。 現在我想在新域上添加另一個rails應用程序,但我對所需的步驟感到困惑。

在上面的設置中,site-available或/etc/nginx/nginx.conf沒有變化。 唯一的配置是在我的apps配置目錄中的unicorn.rb,unicorn_init.sh和nginx.conf中。 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;
}

在我的Capistrano食譜中,我有這條線

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

添加第二個域只是在監聽和取消注釋server_name部分之后刪除默認延遲,然后使用不同的上游套接字名稱和第二個應用程序的服務器名稱重復此配置文件? 是否可行或是否需要將此文件傳輸到可用的站點並創建啟用站點的符號鏈接?

在unicorn.rb中:

申請1:

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

申請2:

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

在nginx.conf中:

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

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

使用Nginx和Unicorn在一台主機上托管不同的應用程序非常容易。

通過定義每個應用程序的socket文件的不同名稱,可以獲得分離。 當然,您應該在nginx.confserver部分指出正確的current/public目錄。

最后一次觸摸在unicorn_init.sh文件中:在它的頂部,您應該使用應用程序的current/public目錄的完整路徑更改APP_ROOT

如果你的設置類似於RailsCast的設置,那么所有其他的事情都由capistrano完成。

我在這里提出的問題上回答了這個問題:

使用nginx +獨角獸的多個Rails 4應用程序

但這是一個答案:

我正在使用Ruby 2.0Rails 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 ,intead / just

但還有更好的方法

那么,上面的內容將適用於生產服務器,但是開發呢? 您是否要正常開發然后在部署時更改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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM