Serving multiple Rails apps with passenger + Nginx on a single domain - ruby-on-rails

I have a single Nginx instance with passenger and would like to serve different Rails apps at different routes. Specifically: /api should serve one app and / should serve a different.
My Rails apps are located at /srv/api/ and /srv/ui on the filesystem.
My Nginx config is currently like this:
user foo;
worker_processes 1;
events { worker_connections 1024; }
http {
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-4.0.49;
passenger_ruby /home/monolith/.rvm/gems/ruby-2.1.1/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /srv/ui/public/;
passenger_enabled on;
}
location /api {
root /srv/api/public/;
passenger_enabled on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
With this config, the API app is being served correctly, but the UI app is not. It returns a 500 error, and there are no error logs in either Nginx or under logs in Rails.
Attempted solutions / debugging
echo 'test' > /srv/ui/public/index.html. This results in a successful render of 'test'
when visiting <hostname>.com/
Changing location / to serve a static index.html using the alias directive instead. This works also.
I saw this solution of symlinking a file inside the / location https://www.chiliproject.org/boards/1/topics/545, but this would be relevant if the API app was not being served.
I suspect this has something to do with interference among passenger instances, but I don't know what the solution is.

We've achieved this before (albeit with Apache - maybe we can work towards a port for Nginx):
#app/apache2/apache2.conf
<VirtualHost *:80>
ServerName *********.co.uk
DocumentRoot /apps/[main_app]/current/public
<Directory /apps/[main_app]/current/public>
Allow from all
Options -MultiViews
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
#Secondry App
Alias /[app_name] /apps/[app_name]/current/public
<Location /[app_name]>
PassengerAppRoot /apps/[app_name]/current
RackEnv production
RackBaseURI /[app_name]
</Location>
</VirtualHost>
In terms of Nginx, you may be able to get away with the following:
#etc/nginx/nginx.conf
http {
server {
listen 80;
server_name yourdomain.com;
#serve your "main" site here
root /srv/ui/public/;
passenger_enabled on;
#serve your "API" app from the location / Alias
location /api {
root /srv/api/public/;
passenger_enabled on;
}
}
}

Related

SSL rails nginx

I am trying to install a SSL certificate that I recently acquired from GoDaddy. My web application is on Rails 4.2.6 and I am using an Ubuntu Server 14.04. I am also using Phusion Passenger 5.0.28 and Nginx. I don’t know if it makes any difference, but I launched the instance using AWS’ EC2.
I created a combined file using the two .crt files sent by GoDaddy.
When I edit my application.rb file:
config.force_ssl = true
I receive the following error:
ERR_CONNECTION_TIMED_OUT
There are two files that I have tried editing, with not success so far:
nginx.conf. The server block currently look like this:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /var/www/primeraraiz5/primeraraiz_combined.crt;
ssl_certificate_key /var/www/primeraraiz5/primeraraiz.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
include /etc/nginx/sites-enabled/*;
rails.conf (in a sites-available directory; which is “symbolically linked” to the sites-enabled directory ). The server block looks like this:
server {
listen 443 ssl;
passenger_enabled on;
passenger_app_env production;
root /var/www/primeraraiz5/public;
server_name 52.39.200.205 primeraraiz.com;
}
server {
server_name www.primeraraiz.com;
return 301 $scheme://primeraraiz.com$request_uri;
}
I don’t know if I am doing something wrong in these files or if I should change any settings at AWS or with the company that currently hosts my domain.
Thanks a lot for your help!
There are a couple of things to do to your configuration.
The first is the server block containing the redirect. Since you haven't provided us with a server that's listening on port 80, I assume that you want to redirect all requests to http://www.primeraraiz.com; to HTTPS. If so, replace $scheme with https so that your block looks as follows:
server {
server_name www.primeraraiz.com;
return 301 https://primeraraiz.com$request_uri;
}
Next, the SSL offloading needs to happen in the server block from which you're serving. In your case, you're offloading SSL for server name localhost, and not for primeraraiz.com which is what I assume you're trying to do. So copy the SSL parameters of your first server block to the one that has server name primeraraiz.com to end up with:
server {
listen 443 ssl;
server_name 52.39.200.205 primeraraiz.com;
ssl_certificate /var/www/primeraraiz5/primeraraiz_combined.crt;
ssl_certificate_key /var/www/primeraraiz5/primeraraiz.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
passenger_enabled on;
passenger_app_env production;
root /var/www/primeraraiz5/public;
}

Digitalocean - More than one application running on the same droplet?

Is it possible to run more than one Rails application on the same DigitalOcean droplet?
I recommend looking into using Dokku with Docker, which allows you to host applications along side each other. Digital Ocean has a One-click install available. I just started using it and deploying this way, and so far really like it.
Here are some links:
http://reallybusywizards.com/dokku-digitalocean-your-very-own-cheap-heroku-clone/
https://www.andrewmunsell.com/blog/dokku-tutorial-digital-ocean
https://www.digitalocean.com/community/tutorials/how-to-use-the-dokku-one-click-digitalocean-image-to-run-a-node-js-app
Yes you can do this, you just need to configure your application server, I have done this using nginx, is very quiet.
This tutorial is pretty cool to start with the server installer and rails application using Nginx server application:
Tutorial DigitalOcean
After doing this, open the configuration file for nginx:
sudo nano /opt/nginx/conf/nginx.conf
Now just add another block to configure a new application on another port, the default port is always 80. Enter note that port 8080 in this block.
server {
listen 8080;
server_name example.com;
passenger_enabled on;
root /var/www/my_new_rails_app/public;
}
Hope this helps!
YES
I am doing this currently. If your using Apache then in your httpd.conf file simply make two entries pointing to the public folders of two different application. Remember to identify different address for each.
I use phusion-passenger to make rails run with apache and my setup looks like this;
<VirtualHost ####################.com:80>
ServerName ####################.com
DocumentRoot /var/www/html/first_app/current/public/
<Directory /var/www/html/first_app/current/public>
Allow from all
Options -MultiViews
</Directory>
PassengerEnabled on
#RewriteEngine On
#RewriteCond %{HTTPS} on
#RewriteRule (.*) http://www.####################.com%{REQUEST_URI}
SetEnv GEM_HOME /usr/lib/ruby/gems/1.8
</VirtualHost>
<VirtualHost second_app.####################.com:80>
ServerName second_app.####################.com
DocumentRoot /var/www/html/second_app/current/public/
<Directory /var/www/html/second_app/current/public>
Allow from all
Options -MultiViews
</Directory>
PassengerEnabled on
#RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://www.####################.com%{REQUEST_URI}
SetEnv GEM_HOME /usr/lib/ruby/gems/1.8
</VirtualHost>
I had similar problem but #Leandro Figueredo answers didn't work for me.
Below i presenting what i do to achieve this.
Actually i have two website on one droplet.
I configured my server with this tutorial: GoRails How to setup server with Ubuntu 14.04 and nginx
After this configure file /etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name example.com www.example.com;
passenger_enabled on;
rails_env production;
root /home/user/appname/current/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
listen [::]:80;
server_name second_site.com www.second_site.com;
passenger_enabled on;
root /home/user/second_app/current/public;
}
Important:
remove default_server; from first server block

Nginx for multiple apps with different subdomain

Been trying to search for a time but can't seem to find a solution. I want to set multiple apps in one server via nginx. Current nginx.conf is as follows:
server {
listen 80;
server_name mydomain.co;
root /mydomain/current/public;
passenger_enabled on;
rails_env production;
}
server {
listen 80;
server_name testing.mydomain.co;
root /mydomain-test/current/public;
passenger_enabled on;
rails_env test;
}
It serves up the correct environment (test) but doesn't serve the correct codebase. I checked the directory mydomain-test via ssh and it contains the recent code but is not being served by nginx.
Basically, what I want is:
mydomain.co to serve /mydomain/current/public
testing.mydomain.co to serve /mydomain/current/public
How is this done correctly?
I believe the root directive should be in the location section. I am unsure of the rails directive and passenger_enabled directive but the rest is similar to what I use and works. Might be best to also specify the index script (i have it set to index.php but change as appropriate).
Once you have made the changes be sure to restart nginx.
server {
listen 80;
server_name mydomain.co;
location / {
root /mydomain/current/public;
index index.php;
}
passenger_enabled on;
rails_env production;
}
server {
listen 80;
server_name testing.mydomain.co;
location / {
root /mydomain-test/current/public;
index index.php;
}
passenger_enabled on;
rails_env test;
}

Multiple Ruby apps (Rails and Sinatra) deployed using Passenger for Nginx?

I have two Ruby applications, one is under Rails and the another is under Sinatra.
How can I deploy both these apps in Nginx and Passenger with one in the root ("localhost:3000") and the other in subroot ("localhost:3000/test")?
The Rails application is running with this configuration. Everything seems to work OK:
server {
listen 80;
server_name localhost;
location / {
root /var/www/demo/public;
passenger_enabled on;
rails_env production;
}
location /test/ {
root /var/www/test/public;
passenger_base_uri /test/;
proxy_pass http://10.0.3.12:80/test/;
passenger_enabled on;
}
I am not able to access the second application.
The server returns 404 for the second app and the first app is still running.
I believe you need to define local servers, that only listen on local port and define your passenger apps there. Your actual server listening on port should only act as proxy.
server {
listen localhost:8181;
server_name test_app;
root /var/www/test/public;
passenger_enabled on;
}
server {
listen localhost:8182;
server_name demo_app;
root /var/www/demo/public;
passenger_enabled on;
rails_env production;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8182/;
}
location /test/ {
proxy_pass http://localhost:8181/;
}
}
I didn't have chance to test this config, so it might have some minor flaws, but it should be correct in high-level terms.
In nginx.conf:
server {
listen 80;
server_name localhost;
location / {
root /var/www/new/public;
passenger_enabled on;
rails_env production;
}
location /test {
root /var/www/demo;
passenger_base_uri /test;
passenger_enabled on;
}
Add a soft link:
ln -s /var/www/loggerapp/public /var/www/new/test

Rails+passenger+nginx run app with url without :port

I have a rails app... I installed chain: nginx+passenger and run rails server. But my trouble is that in browser i must to set up url like:
page.com:3000
but how to use only page.com?
I can't run command passenger start -e=development -p=80 of user restriction....
My nginx conf file is such:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#root /home/prog/OnlineAuto/Shop/public;
#passenger_enabled on;
#access_log logs/host.access.log main;
location / {
root /home/prog/OnlineAuto/Shop/public;
index index.html index.htm;
passenger_enabled on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
So how can i get my rails app by domain without any port? (but run rails server on 3000 port)
You're trying to start Passenger on the same port that Nginx is using which is likely why you're getting an error.
I'm more familiar with Unicorn, but based on the documentation I've read, you shouldn't have to start Passenger in a separate process. With Passenger installed properly, I think you only need Nginx directives to make it work.
Configure your passenger_root and passenger_ruby in http block in nginx.conf, and then
http {
passenger_root /<path_to_passenger_install>;
passenger_ruby /usr/local/bin/ruby;
server {
listen 80;
server_name page.com;
charset utf-8;
root /www/page.com/public;
passenger_enabled on;
rails_spawn_method smart;
rails_env development;
}
}
If you are using passenger here is what I had to use to get it working on www.mysite.com without using www.mysite.com:80 on a centos server:
In etc/httpd/conf the key was to uncomment the NameVirtualHost *:80 and change the * to my server's IP address. Make sure Listen 80 is uncommented. Also add your ip to the VirtualHost tag. It must be running on port 80, not 8080 or something of your choosing.
NameVirtualHost xx.xx.xx.xx:80
Listen 80
<VirtualHost xx.xx.xx.xx:80>
ServerName www.mysite.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /var/www/vhosts/mysite.com/httpdocs/public/
<Directory /var/www/vhosts/mysite.com/httpdocs/public/>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>

Resources