问题
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" => I get a 404 error (blank page)
"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")
What is happening???
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!
回答1:
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/;
from (A request URI is passed to the server as follows): http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
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
回答2:
I don't like putting multiple configs in one file, also, you are better off using virtual hosts. Just add the fake domains to yours hosts file. Here is how you go about that:
http://articles.slicehost.com/2009/4/17/centos-nginx-rails-and-thin
I will add to that that I like to put each server config in its own file and call it something like domain1.com_server.conf
Update:
I tried your config and it works. The only part I changed was added / at the end as other answer suggested. You may still have other problems. That is why I pointed you to the blog post. There are problems the way you are doing it, but it will be okay if you fix them. For example, you need to tell Rails that you are not running on the root domain but at /website1 or /blog. You also need to fix any html links that assume resource paths start at the root. That is why virtual hosts offer a cleaner solution.
来源:https://stackoverflow.com/questions/33403358/multiple-rails-apps-over-nginx-reverse-proxy