2

我正在寻找一个带有独角兽的 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;
    }
  }
}
4

1 回答 1

7

我懂了!事实证明这真的很简单,我在我的博客上写了一篇关于它的文章。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;
  }

现在您可以将 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,而不仅仅是/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 根。

于 2013-08-10T11:58:57.737 回答