Nginx hosting a single Rails app on sub uri - ruby-on-rails

Background:
I am running a debian 7 server behind a reverse proxy.
I have a rails 4 app running ruby 2.0.0-p247
I am using nginx
Server is accessed by navigating to: server-name.foo.dev where foo.dev is the internal reverse proxy domain.
I am unable to use passenger to deploy the app.
This will be the only app running on the server.
Problem:
I need to host the rails app on a sub uri or context root path on the server behind the reverse proxy, so that when users navigate to the website, the url for the root path looks like this:
server-name.foo.dev/rails_app, where rails_app would be the root of the rails app.
How would I set this up in nginx sites-enabled config file, and is there anything I would have to modify on the rails app to allow it to sever the correct paths to static content.

I strongly recommend to use Unicorn instead of Passenger.
You can set up both of them(Unicorn and Nginx) as reverse proxy.
This link will be pretty good paper for you..
Again, you 'rewrite'(Nginx route feature) /rails_app to rails application's Unicorn socket file(usually using upstream).
See also below code snippets.
partial nginx.conf
location ~* ^/(rails_app)/ {
root /your/rails/home;
index index.html index.htm;
proxy_pass http://socket_proxy_name;
}
partial snippet for proxy_pass
upstream socket_proxy_name{
server unix:/your/socket/paht.sock fail_timeout=0;
}

Related

How to deploy an ember-cli app that interacts with a rails API backend to a VPS

I have completed development of an ember.js app for the time being, and am wondering how I can deploy it to a production server? I successfully built a dist dir within the app, and I also cloned the ember app repo to the production server.
I am deploying the rails app with capistrano from my local box to the VPS, and everything appears to be working there. Side note, I am using Nginx as the web server, and puma as the application server for the rails apps.
Also, I have the local dev versions of the ember / rails app working great on my local box running the below commands,
rails s --binding 127.0.0.1 and,
ember server --proxy http://127.0.0.1:3000
So I decided to copy the files that were in the dist dir to the public dir of the rails app, and move the assets for the ember app to the assets dir of the rails app.
On my local dev box, I see CSV files being presented like,
However when I load the "ember app" on the production box I am not seeing the CSV files being presented like,
Which brings me to my question, what is the proper way to deploy a ember-cli app to a production server and have it communicate to a rails API backend?
UPDATE
This is what I am seeing in the network tab,
In an ideal system, I use this setup:
On disk:
/srv/frontend/
/srv/backend/
frontend
With Ember CLI, /srv/frontend contains the output of ember build. I can use the --output-path=/srv/frontend flag to set this, if the Ember CLI source is also on the same machine. All API requests should be prefixed with /api. I do this by setting the namespace property to my ApplicationAdapter to api/ (or api/v1 sometimes.)
backend
/srv/backend contains my backend API (The location doesn't really matter in most cases).
For the Rails API, I use puma as a standalone server. As long as you have a standalone server that listens on a port, it doesn't matter if it's puma or something else. All API routes must be namespaced under /api. you can wrap all your routes in a scope block to do this without changing your codebase. I go one step further and add another namespace v1.
reverse proxy
Then I install nginx and make my config like this:
server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
# add SSL paths
server_name localhost;
# UI, Static Build
location / {
alias /srv/frontend/;
index index.html;
}
# Rails API
location /api {
proxy_pass http://localhost:3000/;
}
}
So now I have an Nginx config that proxies / requests to the /srv/frontend/index.html and /api requests to Puma on port 3000.
The only downside to this is that I'm forced to use hash location on my Ember application. There are ways to circumvent this, but it's just easier to use hash location and history location doesn't really buy me much.

Hosting a rails app inside another website's directory

I have a php site on my server, suppose it is example.com.
And I want to run an rails app inside the domain like example.com/rails.
I am using nginx and tried to edit the example.com's host config and proxied the /rails location to unicorn upstream. But that did not work.
Is it possible to do like example.com/rails ?

Rails Capistrano: Steps after Deployment (Getting remote server up)

Ok. This should be my easiest stackoverflow post yet.
So I have Capistrano installed and configured properly. I've managed a successful deployment to my remote server (incidentally that remote server is running rails 4.0 and the local one was on 3.2.13). All my files appear to have been successfully transferred to my liquid_admin/current directory (they used to just be in the liquid_admin directory... but whatever.)
So what do I do now? How do I get rails server to load the app in liquid_admin/current?
If I try to do "rails server" it just tells me:
usage: rails new app_path
Would that actually overwrite my old app? Basically all I want to do is load the app in the "current" directory. Run the server. Should be a no-brainer right? :)
For a single website on a small server, passenger and Ngnix look like winners.
sudo passenger-install-nginx-module
And then on the Nginx sites folder:
server {
listen 80;
server_name www.mysite.com;
root /rails_website_root/public;
passenger_enabled on;
}
Then just start Ngnix (usually you put it on autostart)
The default server that you probably use in development - WEBrick - is not suitable for production. Some options that you have are:
Unicorn
Thin
You also need Apache or Nginx 'in front' of your Rails server.
All this is well explained in tons of guides, books, railscasts etc, so please go and google it.

Hosting Multiple websites on nginx similar to apache

I am trying to host multiple websites in nginx similar to apache. In apache we have folder called htdocs. We put directories and files inside htdocs and if the URL is something like
www.example.com
then we can access the directories and files inside the htdocs folder as
www.example.com/exampledir1 and www.example.com/exampledir2
I want to do the same with nginx. Is it possible. I have a server in my company which has a static IP. Ive installed nginx on it and by default i have hosted one rails app on it with the following lines in the nginx.conf file
server {
listen 80;
server_name mycomp.does-it.net;
root /var/www/mysamplerails/public;
passenger_enabled on;
rails_env development;
}
This site is accessible to me if i go to the url "mycomp.does-it.net"
now what i would like to do is have another rails app and access it from mycomp.does-it.net/mysecondrailsapp as the root.
Ive googled a lot and im ending up no where. I'm from apache background and these are some of my first tries in nginx.
Use passenger_base_uri option. See documentation: Deploying to a sub URI

How do I remove www from site urls under unicorn web server stack?

I have a rails application hosted on engineyard cloud using unicorn web server stack. I am trying to remove www from the site url but still have no luck. I used to provide RewriteRule in .htaccess for my apache application but I am not sure about unicorn/nginx.
This will redirect any requests for http://www.abc.de to http://abc.de:
server {
server_name www.abc.de;
rewrite ^/(.*) http://abc.de/$1 permanent;
}
server {
server_name abc.de;
# rest of the config goes here
}
You'd normally put this in your vhost config at /etc/nginx/sites-available/site_name; the equivalent on EngineYard appears to be /etc/nginx/servers/app_name/custom.conf, but don't quote me on that.

Resources