Hosting Multiple websites on nginx similar to apache - ruby-on-rails

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

Related

Nginx location app urls

I'm trying to get routing within the nignx config working. I have an app at http://app1:8081 and another app at http://app2:8080. (FYI I'm using docker containers so each app is in its own container) What I have working is for nginx is app1 is point to http://example.com. Where I'm having trouble getting http://example.com/gc to work.
server {
listen 80;
server_name http://example.com;
location /gc/ {
proxy_pass http://app2:8080/;
}
location / {
proxy_pass http://app1:8081/;
}
}
I've tried the proxy_pass with and without trailing / and the location with and without trailing /. I've had an odd result where going to example.com/gc/ would rewrite to example.com/home which didn't work.
I'm was hoping for something that is similar to IIS with application folders under a site. If you have a site that points to example.com and put an application named gc and point it to the application folder.
The end result should be example.com/gc/home renders app2:8080/home.
Any help with my nginx config would be greatly appreciated.

Nginx Passenger not serving Rails application

I setup Nginx with passenger on centos 7 VPS. I installed nginx and passenger as a gem. In addition I installed passenger-install-nginx-module.
When I start sudo service nginx and type passenger-status I get "Phusion Passenger is currently not serving any applications.". From my nginx conf file
http {
passenger_root /usr/local/rvm/gems/ruby-2.4.1/gems/passenger-5.1.8;
passenger_ruby /usr/local/rvm/gems/ruby-2.4.1/wrappers/ruby;
passenger_app_env production;
passenger_instance_registry_dir /var/lib/passenger-instreg;
...
server {
listen 80;
server_name localhost;
root /home/myuserhere/current/public;
passenger_enabled on;
File passenger.* creates normally when I restart nginx.
echo $PASSENGER_INSTANCE_REGISTRY_DIR returns the same path as in nginx conf file.
What I do wrong that passegner does not start application?
Finally I got it. Everything was all right. Passenger does not start apps during startup, but during the first request so all I needed to do it was press enter in my browser ... Hope that answer save time to others.
I was having a similar problem; where passenger wasn't serving any applications. This is on Focal, rails 6, Ruby 3.0.1, passenger-version: 6.0.8.
Turns out that it will start serving requests upon first request. So all I did was to temporarily allow http on the UFW then used 'curl' to http a request. Once the landing page was displayed, I was good to go.

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.

Nginx hosting a single Rails app on sub uri

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;
}

Nginx - Basic Configuration

I've just deployed a Rails application using Capistrano under /home/username/app_name.
Now, I'm not quite sure on how to setup Nginx, I've followed this tutorial, http://coding.smashingmagazine.com/2011/06/28/setup-a-ubuntu-vps-for-hosting-ruby-on-rails-applications-2/comment-page-1/#comment-594321. I have this on my nginx.conf file, I modified the first server block I saw:
server {
listen 80;
server_name www.yourdomain.com;
root /home/johndoe/test_app/current/public;
passenger_enabled on;
...
}
But now, I'm not sure how to procede. What exactly is the server_name in the example above? And how should I access the application? I've tried typing in my ip address and nothing shows up. I'm using Linode by the way. And how do I set up the nameservers for my domain? Detailed explanations and tutorials would be very helpful. Thanks in advance!
server_name on Nginx is exactly the same as ServerName in Apache, i.e. the domain name you'd like to use for this directory (named virtual host).
With passenger set to on you should be able to access the application directly via the domain.

Resources