ruby-on-rails - 使用 nginx + unicorn 的多个 Rails 4 应用程序

标签 ruby-on-rails nginx unicorn

<分区>

我正在寻找用 unicorn 设置一个 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;
    }
  }
}

最佳答案

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

内容如下:

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

在您的 nginx.conf 文件中,我们将使 nginx 指向一个 unicorn 套接字:

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

然后,在您的服务器监听端口 80 的情况下,添加一个位置 block ,指向您的 Rails 应用程序所在的子目录(这段代码,必须在服务器 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;
  }

现在你可以将 Unicorn 作为守护进程了:

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

关于ruby-on-rails - 使用 nginx + unicorn 的多个 Rails 4 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134046/

相关文章:

php - 使用 Nginx 时未指定输入文件

linux - 如何在 docker 实例中关闭 sendfile

django - J2ME 的 HTTP 411 错误 -> nginx + apache + django

ruby-on-rails - 如何让 unicorn 在路径下运行 Rails 3.0 应用程序?

ruby-on-rails - 设计首次使用 Rails 登录

javascript - 回形针默认 url 图像在 Rails 5 中不起作用

ruby-on-rails - Rails 中的条件查询

ruby-on-rails - 在开发模式下自动刷新 Rails Metal

ruby - 具有 unicorn 和 memcached 的 Sinatra 应用程序应该为缓存命中返回 304 代码吗?

ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境