【问题标题】:Multiple Rails 4 app using nginx + unicorn [duplicate]使用 nginx + unicorn 的多个 Rails 4 应用程序 [重复]
【发布时间】:2013-08-10 15:20:42
【问题描述】:

我正在寻找一个带有独角兽的 nginx 服务器。我设置了第一个应用程序,但它位于根“/”上。我真正想要的是输入 localhost/app1 它会运行,而如果只是进入根目录,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;
    }
  }
}

【问题讨论】:

标签: ruby-on-rails nginx unicorn


【解决方案1】:

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

这是内容:

我正在使用 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 的情况下,添加一个 location 块,指向您的 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;
  }

现在你可以把独角兽当恶魔了:

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 根。

【讨论】:

  • 很棒的解决方案!!我一点改进:你不需要RouteScoper。一个带有救援的表达式是好的,恕我直言。 root_dir = Rails.application.config.root_directory rescue '/'。然后你做scope root_dir do ... end
  • 可以做个短视频教程吗?
猜你喜欢
  • 2012-12-19
  • 1970-01-01
  • 2015-08-15
  • 2014-11-08
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
相关资源
最近更新 更多