Nginx with Phusion Passenger URI routing - ruby-on-rails

This is my Nginx Server config file
server {
listen 80;
location /node {
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/proxy.conf;
}
location /ror {
root /var/www/ror/public;
passenger_enabled on;
proxy_cache off;
proxy_pass_header Server;
}
location / {
root /usr/share/nginx/www;
}
}
This is mostly a 'HelloWorld' setup to test running Node and RoR simultaneously for a REST server.
The issue is that I need to have servername.com/ror forward to /var/www/ror/public, inorder for RoR to respond. I have tried a few configurations, and cannot figure out how to get it to forward correctly. The issue seems to be that the ror URI segment gets passed on to RoR, which causes it to look for that controller, which returns a 404.
The other configuration, changing root to /var/www works as long as I include /ror/public in the URI.
So, is there a way to perform the sub URI routing on the reverse proxy in this manner?
Ok, so I added the rewrite rule.
location /ror {
root /var/www/ror/public;
rewrite ^/ror/(.*)$ /$1 last;
passenger_enabled on;
proxy_cache off;
proxy_pass_header Server;
}
The issue, however, is that now it is serving files from the default root, which in my install is /usr/nginx and is completely ignoring the root inside the location block.
**I appreciate the help. This is my first time with Nginx (Typically an Apache Fiend), so I'm having trouble wrapping my head how it's interpreting these commands.

/ror/a.txt is /var/www/ror/public/ror/a.txt in local fs, so you need to use rewrite rule in location /ror.
Like rewrite ^/ror(.*)$ $1;

I eventually figured it out.
My VHOST Section:
server {
#server_name domain.com;
listen 80;
root /var/www/rails;
passenger_enabled on;
passenger_base_uri /todo;
rails_spawn_method smart;
rails_env development;
proxy_cache off;
proxy_pass_header Server;
location /node {
proxy_pass http://127.0.0.1:8080;
include /etc/nginx/proxy.conf;
}
}
I ended up creating a new directory called rails, and inside of that I created a symbolic link to the public directory of the actual application.
ln -s /var/www/todo/public /var/www/rails/todo
Also, I was not initializing Passenger correctly, which is why it was not responding and giving back errors. This setup is not ideal, considering I now have to manually add each sub URI application, but it works.

Related

Point Nginx to one page of my rails app

I have a domain on namecheap and I created a subdomain for the staging env, nevertheless the point of me doing that because I want to have this domain to point to one page in my rails app but I'm not sure how to do that.
so I went to staging and I nano the file in sites-enabled directive and added this block:
server {
listen 8880;
server_name staging.my_domain_name.com;
access_log /var/www/my_app_name/current/log/access.log;
error_log /var/www/my_app_name/current/log/error.log;
root /var/www/my_app_name/current/public/;
passenger_ruby /home/deploy/.rbenv/versions/2.1.0/bin/ruby;
passenger_enabled on;
rails_env staging;
rails_spawn_method smart-lv2;
passenger_min_instances 1;
# Maintenance
error_page 404 /404.html;
error_page 403 /404.html;
location = /404.html {
internal;
}
}
so for the root I tried to change the path to my view path
/var/www/my_app_name/current/app/views/pages/page_name.html.erb;
I also tried to add the link as it is
I'm not sure how to do this and I'm still new in DevOps
I tried to look for how to make Nginx point to one page in my rails app
Note: It listens to 8880 because i have varnish server who listen to port 80
Any help would be highly appreciated.
Thanks.

How to configure Nginx to serve both Rails app and Angular on same domain

I'd like to use Nginx to serve both my Rails (using passenger) and an Angular front end.
Basically, all requests that goes to / should be directed to Rails and every request that goes to /admin/* should be redirected to a Angular App.
I've found this question that would theoretically be exactly what I'm looking for. Unfortunately Dmitry's answer is a bit vague for someone (like me) who doesn't know much (next to nothing) about Nginx.
EDIT: Here's the Nginx configuration that I have tried:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 178.62.92.199;
passenger_enabled on;
passenger_app_env development;
root /var/www/netturing/public;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location ~ ^/admin/ {
proxy_pass http: /var/www/client/admin;
}
location / {
proxy_pass http: /var/www/netturing/public;
}
}
Could someone extend the answer a bit?
Thanks!
I think haproxy it's a better tool to do this.
But I have some rails apps in standalone passenger server with nginx in front.
My suggested nginx config:
server {
# some config directives
...
# Al turrón !
# Angular App
location ~ ^/admin/ {
proxy_pass http://<URI TO ANGULAR APP>;
}
# Rails App
location / {
proxy_pass http://<URI TO RAILS APP>;
}
}

nginx - how to create rails app without app name in domain

I'm using nginx with passenger and I wanted to ask, how to create rails app, without showing app name in domain.
Currently I know how to setup nginx configuration with domain like this:
domain.com/app_name
and in the routes file when I create root 'welcome#index'
I still have to type domain.com/app_name to view my controller, but how to make it display welcome controller when the domain is simply domain.com
my nginx configuration looks looks this:
server {
listen 80 default_server;
server_name domain.com;
root /home/username/rails;
location ~ ^/app_name(/.*|$) {
alias /home/username/rails/app_name/public$1; # <-- be sure to point to 'p$
passenger_base_uri /app_name;
passenger_app_root /home/username/rails/app_name;
passenger_document_root /home/username/rails/app_name/public;
passenger_app_env development;
passenger_enabled on;
}
}
when I try to comment out #passenger_base_uri /app_name I got 403 ERROR code.
Any help would be appreciated.
You need to remove the location block, alias, passenger_base_uri, passenger_app_root, and passenger_document_root. Change root to your Rails public directory too. It should look like this:
server {
listen 80 default_server;
server_name domain.com;
root /home/username/rails/app_name/public;
passenger_app_env development;
passenger_enabled on;
}
Try to follow this example of this production nginx config, which also includes some best practices for configuring your server: https://gist.github.com/mikhailov/711913

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

nginx proxy_pass with passenger

I can't seem to make this nginx config work. I have a rails app, and I need to proxy everything under a path to a Java/Tomcat setup. I have another route (which does hit the main rails app) under lockdown and that works, but the proxy_pass doesn't; it just hits the main app.
It seems that when I remove the lockdown location directive, it works.
Reordering the two location directives has no effect.
The basic config:
server {
listen 80;
root /rails/app/public;
rails_env development;
passenger_enabled on;
location /JavaApp {
proxy_pass http://127.0.0.1:8080/JavaApp/;
}
location /lockdown {
# Have to re-enable passenger
passenger_enabled on;
allow 127.0.0.1;
deny all;
}
}
What am I doing wrong?
I thought I had tried every combination, but I went through a bunch more and found that this worked:
location ~ \.jsp$
Which boggled my mind because before it whined about not allowing regex in the location with proxy_pass, but I had a path in the proxy pass line... SO!
This does the trick
location ~ /JavaApp {
proxy_pass http://127.0.0.1:8080;
}
Try removing passenger_enabled pn; from the top level of the server {} block and I think it will start working for you.

Resources