【问题标题】:How: Multiple Rails apps per VHost - Migrate from Passenger RailsBaseURI?如何:每个 VHost 多个 Rails 应用程序 - 从乘客 RailsBaseURI 迁移?
【发布时间】:2012-05-20 05:40:14
【问题描述】:

我正在从 apache/passenger 迁移到 nginx/unicorn。我已经成功加载了 nginix 并使用 unicorn 托管了多个站点,但每个域只有一个。

在Passenger中,可以通过子文件夹在一个域下托管多个rails应用程序,并继续在根目录下托管一个html文件。例如:

<VirtualHost 10.0.0.2>
    ServerName preview.site.com
    ServerAlias preview.site.com

    DocumentRoot /var/www/com.site.preview

    <Directory /var/www/com.site.preview>
            AllowOverride all
            Options -MultiViews
    </Directory>

    RailsBaseURI /app-one
    RailsBaseURI /app-two
    RailsBaseURI /app-three

</VirtualHost>

所以去

http://preview.site.com

会给我一个目录。去:

http://preview.site.com/app-one
http://preview.site.com/app-two
http://preview.site.com/app-three

将改为运行该子文件夹下的每个应用程序。有没有我仍然可以在 nginx/unicorn 中执行此操作的方法?

How to make unicorn run a Rails 3.0 app under a path? 上提出了类似的问题,但只提供了部分答案。

【问题讨论】:

  • 这不是 nginx 配置.. 看起来像 apache2。
  • ?那是因为,这正是我要问的?! ...“我正在从 apache/passenger 迁移到 nginx/unicorn。”...
  • 抱歉,这才一个月大,我这辈子都记不起我发帖时脑子里的想法了。

标签: ruby-on-rails nginx rack unicorn


【解决方案1】:

您需要做的是在您的 nginx 配置中定义locationsserver 块定义了 nginx 应用程序在哪个端口上运行,以及它侦听的 URL。在该块内,添加以下内容:

location /app-one/ {
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:3000;
}

location /app-two/ {
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:4000;
}

location /app-three/ {
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:5000;
}

这假设了以下几点:

  • 您的应用与 nginx 在同一机器上运行
  • 您的应用在上述三个端口上运行:3000、4000、5000

这种方法是一种反向代理,是 Nginx 最常见的用途之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多