nginx proxy_pass with passenger - ruby-on-rails

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.

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 with Phusion Passenger URI routing

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.

Running the same Rails app on localhost on different domains and on a different Environment

I want to run the same Rails 3 app at urls: davinci.local.dev:8081 davinci.testing.dev:8082 and with environments development and testing, respectively.
I am using Nginx and passenger. My first attempt was:
server {
listen 8081;
server_name .davinci.local.dev;
root /Users/Nerian/NetBeansProjects/DaVinci/DaVinci/public;
passenger_enabled on;
rails_env development;
#rails_env development
}
server {
listen 8082;
server_name .davinci.testing.dev;
root /Users/Nerian/NetBeansProjects/DaVinci/DaVinci/public;
passenger_enabled on;
rails_env test;
#rails_env development
}
Yet when I browse to said urls, I get to the same deployment. If I first request the testing one, then it is that the app that is served always, whether I browse to davinci.local.dev or davinci.testing.dev. If I first request the development one, then it is that one which is served always.
Both deployments are using the same root app. Is there a way to make this works?
The objective is to serve the same app at different ENV in different domains.
I had a similar problem. I wanted a Rails application (named apparat) to have two nginx instances: example.eu for english version of the site and example.ee for estonian version. They share exactly the same functionality, so it makes sense to have just one code base for it (but I do have to make sure static assets like user-uploaded images won't collide).
server {
listen 80;
server_name example.eu;
root /home/apparat/public;
passenger_enabled on;
rails_env apparat_eng;
}
server {
listen 80;
server_name example.ee;
root /home/apparat/public;
passenger_enabled on;
rails_env apparat_ee;
}
However, similar to above, if I accessed example.ee, I got the example.eu instance of my Rails app. Possibly some quirk in nginx / passenger?
Anyway I got around by making an arbitrary symlink to /home/apparat, for instance
ln -s /home/apparat /home/apparat_eng
ln -s /home/apparat /home/apparat_ee
and changed the nginx config respectively
server {
listen 80;
server_name example.eu;
root /home/apparat_eng/public;
passenger_enabled on;
rails_env apparat_eng;
}
server {
listen 80;
server_name example.ee;
root /home/apparat_ee/public;
passenger_enabled on;
rails_env apparat_ee;
}
So I'm able to trick nginx into thinking these are different directories.
I took a different approach. The development server is served by nginx using passenger while the testing server is started on demand when using rake spec:acceptance.
So nginx.conf is:
server {
listen 8081;
server_name .davinci.local.dev;
root /Users/Nerian/NetBeansProjects/DaVinci/DaVinci/public;
passenger_enabled on;
rails_env development;
#rails_env development
}
Just that.
And in spec/support/custom_env.rb
Capybara.run_server = true
Capybara.app_host = 'http://davinci.testing.dev:8082'
Capybara.server_port = 8082
And that's the magic.

Resources