Alter Nginx.conf file to route to rails project - ruby-on-rails

Okay, this is my first ever vps, and this is my hello world test. I'm one step away from displaying my rails application
I have a VPS running Ubuntu.
I have a rails project just sitting here:
(I pulled it from git hub using git, and ran 'bundle install' to install the gems on the vps, haven't used Capistrano at all)
root /home/starkers/rails_application ->
test_app
When I visit my vps' ip in a browser I recieve the Welcome to nginx! page.
Finally,
sudo nano /opt/nginx/conf/nginx.conf
Opens my config file.
Now, I haven't made any headway on this in about an hour. All the online guides show a slightly different nginx.conf structure so I'm a little bit in the dark here.
The nginx.conf file is pretty large, what variables do I need to change in order to make a direct http request to the server route to the rails project?
I think this is what matters for my purposes:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
I'd be most grateful if someone could explin this to me a bit more. I'm betting the index.html referenced here is the "Welcome to nginx!" page. Should a public webserver always listen on port 80? Does the server_name matter?
How can I then tweak the nginx.conf file so that a url I purchased will route the project?
Really wouldn't mind some help here.

This is really a multi-step process. You will need to:
Configure a nginx server block for the domain you want to host your app from.
Configure an application server or other "bridge" between your rails app and nginx. I'd recommend the passenger gem.
If you decide to go the passenger route, you could modify the server block above similar to the following:
passenger_root /somewhere/passenger-x.x.x;
passenger_ruby /usr/bin/ruby;
passenger_max_pool_size 10;
server {
# uncomment the line below if you will be serving multiple domains/websites from this box
# server_name example.com www.example.com;
listen 80;
# point this to the path to your rails app's public directory
root /home/starkers/rails_application/test_app/public;
passenger_enabled on;
}
Source: http://www.modrails.com/documentation/Users%20guide%20Nginx.html
This HOW TO install guide from digitalocean may be of interest to you.

Related

nginx and passenger to run my rails application

I'm new to Rails and trying to deploy my rails app (which runs perfectly on my computer using rails server) on to a server.
My server has nginx and passenger both installed and working fine. However, when I change the nginx config file at /opt/nginx/conf/nginx.conf as follows
....
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/ruby1.9.1;
....
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/path_to_my_app/public;
index index.html index.htm;
}
passenger_enabled on;
}
it doesn't work. I'm not able to call my rails code. It keeps saying 404 error.
I restarted the nginx using service nginx restart which returned OK, but still no luck.
Can anyone tell how to debug this. Where should I check what all applications my passenger is running? What is the part I'm missing?
EDIT: I'm following this documentation very closely
It seems that there is no /home/path_to_my_app/public directory.
In Passenger version 5.0.7 directory access has been changed. Now if root directory doesn't exist it falls back to nginx to search index.html and gives 404 error if there is now such file.
[Nginx] The Nginx module now looks for index.html if the path ends in
/ so that it works intuitively, without needing to use try_files.
https://blog.phusion.nl/2015/04/29/passenger-5-0-7/
If your server log says that passenger agents are online and if you run sudo passenger-status and see your application listed, then it would appear the agent is setup correctly.
Are you trying to access the site using localhost? If you're trying to access it from outside the server, you'll need to set server_name to either your domain or an IP address (and restart nginx after any changes). If you run curl http://localhost on the server, do you see the html from your site, or just another 404?
"location /" means all the HTTP request will be handled by this location's handler but there isn't one so you will get "404 not found". I guess you mean that your home page is index.html so change "location /" to "location =/" will fixed your problem.
update
server {
listen 80;
server_name localhost;
root /path/to/public;
passenger_enabled on;
}
There is no need add location for simple app.

Nginx X-Accel-Redirect config for Rails app

In all the tutorials one may come across for setting up X-Accel-Redirect for nginx, there is always a bit about indicating a certain location is private, like so:
location /protected/ {
internal;
root /some/path;
}
The files I'm sending to my users are in RAILS_ROOT/private_uploads. This is not inside the public folder, meaning that there are no URLs that point to directly to the files in this folder.
So am I correct in assuming I can ignore that bit of nginx config for my setup? If not, what config would I need?
I'm using Passenger, btw, and my config is really basic:
server {
listen 80;
server_name mydomain.com;
root RAILS_ROOT/public;
passenger_enabled on;
}

Nginx Config: one Server name and two Applications

Sorry, this question may have already being answered, but I can't find the answer that will help me.
My situation is like this. We have a interal server, say called "helper.local.company" We currently use it to run a Rails Application. To get there a user just types "helper.local.company/ror_app1_route"
I would like to now install another app on to the server and then alloy the users to only type helper.local.company/ror_app2_route to get to the new app.
my current nginx conf file looks like this:
server {
listen 80 default_server;
server_name helper.local.company;
root /var/www/apps/ror_app1/current/public;
passenger_enabled on;
rack_env production;
}
What would I need to change or add inorder to allow the two rails apps to work with out having to use sub-domains?
Thanks
what keyword you need is 'passenger_base_uri'
reference from:http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deploying_rails_to_sub_uri
The following is a simple example
Suppose that you already have a server virtual host entry:
http {
...
server {
listen 80;
server_name www.phusion.nl;
root /websites/phusion;
}
...
}
And you want your Ruby on Rails application to be accessible from the URL http://www.phusion.nl/rails.
To do this, make a symlink in the virtual host’s document root, and have it point to your Ruby on Rails application’s public folder. For example:
ln -s /webapps/mycook/public /websites/phusion/rails
Next, set passenger_enabled on and add a passenger_base_uri option to the server block:
http {
...
server {
listen 80;
server_name www.phusion.nl;
root /websites/phusion;
passenger_enabled on; # <--- These lines have
passenger_base_uri /rails; # <--- been added.
}
...
}
Then restart Nginx. The application has now been deployed.
You can deploy multiple Rails applications under a virtual host, by specifying passenger_base_uri multiple times. For example:
server {
...
passenger_base_uri /app1;
passenger_base_uri /app2;
passenger_base_uri /app3;
}

how to point to my rails application to my domain name

I'am using Phusion-passenger and nginx to deploy my rails application, when i goto my server IP-Address i am able to access my rails application, but i cant access it when i go to the domain name. my configuration file looks like this;
server {
listen 80;
server_name my_domain.com;
passenger_enabled on;
root /home/user/rails_app/public;
}
i have tried configuring my nginx.conf in so many ways but it wasn't accessing the domain name. i also created a symbolic link to my application root folder like this
ln -s /opt/nginx/conf/sites-available/rails_app /opt/nginx/conf/sites-enabled/rails_app
i also tried doing this
ln -s /home/user/rails_app/public /home/user/rails_app
i updated my nginx.conf file to look like this also
server {
listen 80;
server_name my_domain.com www.my_domain.com;
root /home/user/rails_app/public;
passenger_enabled on;
passenger_base_uri /rails_app;
rails_env production;
}
You can rename the nginx config file for this app to the domain name, I guess it will help you in the future.
For the domain redirect, you need a DNS server (like Bind). If you're using a VPS server (like Linode, Webbynode or correlated), they provide you a structure to do that without installing a DNS server, it's very simple.
Besides that, your configuration is ok.
If you need some tunning, check this out.

How to use different rails_env with nginx, passenger and redmine

I need to have redmine running in combination with nginx, phusion passenger and mysql. Because of the project requires several instances of redmine, which should be realized using different rails_env, I tried to set them in the different server vhosts with nginx.
Example of one vhost:
server {
listen xxxx;
server_name redmine.xxxxx;
root /xxxxx/redmine/public;
passenger_enabled on;
rails_env production;
}
Same goes for the other server vhost, but there server_name is matched to the other domain and rails_env set to internal.
The problem is, that nginx just uses one of both rails_env for both redmine instances, not one for each. Any advice how to use different rails_env with the same application, nginx and phusion passenger?
Thanks
I think you're having the same problem I had. You want to use the same physical directory to host the application instances but you want to interact with the app under different environments (development/production) by using different DNS entries (redmine.development / redmine.production)???
The problem is that passenger recognizes the incoming request as using the rails app found in the directory above root. If you're using the same literal reference for root in multiple nginx configs, passenger will forward the request to the single running instance found in root. i.e., if you start up your development application first, then try to access production via redmine.production, you'll end up interacting with the development environment. However if you start up your production app first, then try to access redmine.development, you'll end up interacting with production.
The answer is to symlink your app's directory for every environment you want to run. Passenger only looks at the literal path to root - if it doesn't match a currently running instance, it'll spawn a new one.
ex.)
Physical root is ~/rails_apps/myserver (where myserver contains app, public, etc.)
Create a symlink called ~/rails_apps/dev.myserver to ~/rails_apps/myserver and another one called ~/rails_apps/pro.myserver to ~/rails_apps/myserver.
Now inside your nginx config, use the symlink locations to the public folder as root.
ex., if symlink /home/user/rails_apps/[dev|pro].redmine points to /home/user/rails_apps/redmine)
server {
listen xxxx;
server_name redmine.development;
root /home/user/rails_apps/dev.redmine/public;
passenger_enabled on;
rails_env development;
}
server {
listen xxxx;
server_name redmine.production;
root /home/user/rails_apps/pro.redmine/public;
passenger_enabled on;
rails_env production;
}
nginx passenger doesnt follow symlink of .../app/public directory many time
because it expect it to be a directory not file
However you can use PASSENGER_APP_GROUP_NAME directive.
like this:-
server {
listen xxxx;
server_name redmine.xxxxx;
root /xxxxx/redmine/public;
passenger_enabled on;
rails_env production;
passenger_app_group_name devlopment;
}
server {
listen xxxx;
server_name redmine.xxxxx;
root /xxxxx/redmine/public;
passenger_enabled on;
rails_env production;
passenger_app_group_name production
}

Resources