Ruby on rails 使用nginx+;独角兽
我正在寻找建立一个与独角兽nginx服务器。I第一个应用程序已设置,但它位于根“/”上。我真正想要的是输入localhost/app1,它将运行,而如果只需输入根目录,html或php页面将被打开 有线索吗 以下是当前的nginx.config:Ruby on rails 使用nginx+;独角兽,ruby-on-rails,nginx,unicorn,Ruby On Rails,Nginx,Unicorn,我正在寻找建立一个与独角兽nginx服务器。I第一个应用程序已设置,但它位于根“/”上。我真正想要的是输入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;
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 2.0和Rails 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的情况下,添加一个位置块,该位置块指向rails应用程序所在的子目录(此代码必须位于服务器块内):
现在你可以让独角兽当执事了:
sudo unicorn_rails -c config/unicorn.rb -D
最后要做的一件事,也是我挖掘得最多的一件事是为rails routes文件添加一个作用域,如下所示:
MyApp::Application.routes.draw do
scope '/myapp' do
root :to => 'welcome#home'
# other routes are always inside this block
# ...
end
end
这样,你的应用程序将映射一个链接/myapp/welcome,intead of just/welcome
但还有更好的办法
好的,上面的内容可以在生产服务器上使用,但是开发呢?您是否要正常开发,然后在部署时更改rails配置?对于每个应用程序?这是不必要的
因此,您需要创建一个新模块,我们将把它放在lib/route\u 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/environments/production.rb上的根目录:
MyApp::Application.configure do
# Contains configurations for the production environment
# ...
# Serve the application at /myapp
config.root_directory = '/myapp'
end
在config/environments/development.rb中,我没有指定config.root\u目录。这样它就使用了普通的url根。非常好的解决方案!!我有一点改进:你不需要一个线路操作员。一个带rescue的表达式就可以了,imhoroot\u dir=Rails.application.config.root\u目录救援“/”
。然后执行scope root\u dir do。。。结束
。您可以制作一个关于的简短视频教程吗?如果您已经知道它是重复的,那么就无需再次回答相同的问题。
MyApp::Application.configure do
# Contains configurations for the production environment
# ...
# Serve the application at /myapp
config.root_directory = '/myapp'
end