NGINX上的多个Rails应用程序(反向代理)

时间:2015-10-28 23:27:40

标签: ruby-on-rails ruby nginx thin proxypass

我的服务器上有两个rails应用程序。它们中的每一个都在瘦服务器上运行。我也在使用NGINX。这是我的NGINX配置文件:

server{

location /blog {
   proxy_pass http://127.0.0.1:8082;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

}
location /website1 {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
}

http://HOST/blog”=>我收到404错误(空白页)

http://[HOST]/website1”=>我从我的Rails应用程序中收到404错误,并在我的应用程序日志中得到:

 INFO -- : Started GET "/website1"
 FATAL -- : ActionController::RoutingError (No route matches [GET] "/website1")

发生了什么事???

我尝试在“/”上设置网站1的位置,在“/ blog”上设置博客。在这种情况下,website1运行完美,但我仍然在博客上获得404(空白页,而不是轨道页)。

有什么想法吗?谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

尝试向代理传递添加尾部斜杠。像:

proxy_pass http://127.0.0.1:8082/;
proxy_pass http://127.0.0.1:3000/;

from(请求URI按如下方式传递给服务器):http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

为了进一步解释为什么你得到404s,当排除尾部斜杠时,rails会看到完整路径(例如http://HOST/website1 - > / website1& http://HOST/blog - > / blog) 。听起来两个应用程序的rails路由不期望前缀。通过在代理传递行中包含尾部斜杠,您的URL将进行转换,使http://HOST/website1/成为rails侧的根路径(/)。如果您的rails重定向无法正常运行,则可能还需要proxy_redirect default;。请参阅:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

答案 1 :(得分:0)

我不喜欢在一个文件中放置多个配置,而且,最好使用虚拟主机。只需将假域添加到您的主机文件中即可。这是你如何去做的:

http://articles.slicehost.com/2009/4/17/centos-nginx-rails-and-thin

我将补充一点,我喜欢将每个服务器配置放在自己的文件中,并将其称为domain1.com_server.conf

更新

我尝试了你的配置,但它确实有用。我改变的唯一部分是在最后添加/其他答案建议。您可能还有其他问题。这就是我向博客发帖的原因。你的方式有问题,但如果你修复它们就没问题。例如,您需要告诉Rails您没有在根域运行,而是在/ website1或/ blog运行。您还需要修复任何假定资源路径从根开始的html链接。这就是虚拟主机提供更清洁解决方案的原因。