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

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

Related

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

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