0

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!

2 Answers2

2

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

Paul Nicholson
  • 557
  • 3
  • 15
  • Thank you! That works way better! I would have never found out that a trailing slash was missing. To make it work, I also added `config.action_controller.relative_url_root = '/blog'` So when I got to http://HOST/blog my root page is displayed. However whenever I click on a link, the prefix is ignored, it goes to http://HOST/posts/1. Do you have any idea? I'm sorry for asking such stupid questions but I really don't know anything about production environment ! –  Oct 30 '15 at 04:37
  • If you set ``config.action_controller.relative_url_root`` then I think you do not need the trailing slash. relative_url_root should take care of url generation as well. – Paul Nicholson Oct 30 '15 at 18:43
0

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.

Bassel Samman
  • 802
  • 5
  • 11
  • Yes, I agree with you, but this is "just" a best practice right? I'm just trying to make it work for now. I still don't know what's the issue here. –  Oct 29 '15 at 15:31
  • It is just a best practice. As far as your particular issue is concerned, I think you are configuring it wrong. That is why I gave you the link. Mainly, setup an upstream, and set your root. – Bassel Samman Oct 29 '15 at 18:49
  • I guess you can make yours work too the way you described. I assume your website1 worked because you were just passing the requests to your thin server running on port 3000. Do you have another thin server running on port 8082? – Bassel Samman Oct 29 '15 at 19:12
  • Okay, updated my answer to kind of mix the configs of a working server I have with your config. If it works, you can mess with it to clean up what you don't need. – Bassel Samman Oct 29 '15 at 19:23
  • Never mind, I removed the update after reading other answer. Basically did the same thing but didn't need to add anything else. All you need to do was add the / at the end of the proxy_pass. Your config will still be broken. But your site will be up and running. You need to set root so your static resources will get served correctly. I still say take a look at that blog post to fix other issues you may see after the website is up and running. – Bassel Samman Oct 29 '15 at 20:46
  • Yes, its not typical for rails apps but it will work just fine. You don't always have the ability to place things under subdomains and sometimes you end up with configurations like this to support old urls or whatever. I have used this type of thing to port a portion of a rails json api to another language for performance reasons. You can still separate the two location directives into separate files if you want. – Paul Nicholson Oct 30 '15 at 01:38
  • Thank you for your help. I'll try to do that the "right" way whenever I get a better grasp of NGINX. –  Oct 30 '15 at 04:40