NGINX上的多个Rails应用程序(反向代理) [英] Multiple Rails apps over NGINX (reverse proxy)
问题描述
我的服务器上有两个Rails应用程序.它们每个都在Thin Server上运行.我也在使用NGINX.这是我的NGINX配置文件:
I have two rails apps on my server. Each of them is running on a Thin server. I am also using NGINX. This is my NGINX configuration file:
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/blog" => I get a 404 error (blank page)
" http://[HOST]/website1 " =>我从Rails应用中收到404错误,并在我的应用程序日志中得到:
"http://[HOST]/website1" => I get a 404 error from my Rails app, and on my app logs I get:
INFO -- : Started GET "/website1"
FATAL -- : ActionController::RoutingError (No route matches [GET] "/website1")
发生了什么事???
我尝试在"/"上设置网站1的位置,并在"/blog"上设置博客的位置.在这种情况下,website1可以完美运行,但是我在博客上仍然看到404(空白页,而不是rails).
I tried setting the location of website 1 on "/" and blog on "/blog". In this case website1 works perfectly, but I still get a 404 on blog (blank page, not a rails one).
有什么主意吗?谢谢您的帮助!
Any idea? Thank you for you help!
推荐答案
尝试在代理通过中添加尾部斜杠.喜欢:
Try adding a trailing slash to proxy pass. Like:
proxy_pass http://127.0.0.1:8082/;
proxy_pass http://127.0.0.1:3000/;
来自(将请求URI如下传递到服务器): http: //nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
from (A request URI is passed to the server as follows): http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
为进一步说明您为什么获得404的原因,当排除斜杠时,rails会看到完整路径(例如 http://HOST/website1 ->/website1& http://HOST/blog ->/blog).听起来这两个应用程序的Rails路由都不需要前缀.通过在代理服务器通过行中添加斜杠,您的网址将得到转换,以使 http://HOST/website1/成为导轨侧的根路径(/).如果您在使用rails重定向时遇到问题,则可能还需要proxy_redirect default;
.请参阅: http://nginx.org/en/docs/http/ngx_http_proxy_module.html #proxy_redirect
To further explain why you are getting 404s, rails is seeing the full path when the trailing slash is excluded (eg. http://HOST/website1 -> /website1 & http://HOST/blog -> /blog). It sounds like the rails routing for both apps is not expecting a prefix. By including the trailing slash in the proxy pass line, your urls will get transform such that http://HOST/website1/ becomes the root path(/) on the rails side. You may also need proxy_redirect default;
if you have issues with rails redirects not working. See: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
这篇关于NGINX上的多个Rails应用程序(反向代理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!