Let Rails trigger update of nginx config? - ruby-on-rails

I'm currently adding user domains to my Rails application. Which means they can use example.com to reach their profile page on my Rails app. I've already implemented this on the Rails side but what's missing is the Nginx side.
How can the Rails app somehow update the Nginx config (add/remove specific domain) and let Nginx reload the config to handle requests from these domains?

Related

Same domain hosting of both static landing page and Rails app on sub-uri with Dokku

I currently have the following scenario on one of my VPSs:
https://example.com - Landing page hosted on root of domain
https://example.com/app - Nginx proxying /app to my Rails app on same VPS (served by Puma)
Need to migrate both to a new server as Dokku apps and that should result in two apps: one for the static landing page and one to the Rails app. Is that even possible? If yes, any tips?
Thanks!
Got it working by setting a reverse proxy on the landing page app's nginx config file, pointing to the main app:
Disable VHOST support on main app, since it won't be reachable outside or by a domain name. This will create a local listening container on a high numbered port
Add a proxy.conf file to /home/dokku/landing-page/nginx.conf.d/
Set up a reverse proxy on a 'location /app/' block inside that file. Set the upstream to the main app's IP and PORT (from the first item).
That's the main steps. Had to tweak forwarded headers a bit to get SSL working fine, but it works fine!

Hosting a rails app inside another website's directory

I have a php site on my server, suppose it is example.com.
And I want to run an rails app inside the domain like example.com/rails.
I am using nginx and tried to edit the example.com's host config and proxied the /rails location to unicorn upstream. But that did not work.
Is it possible to do like example.com/rails ?

Two Rails apps deployed - How do I change the default App?

I've followed Ryan Bate's guide to deploy two completely different rails apps one one VPS (cost saving, using it for development of small home projects). Link to railscast: http://railscasts.com/episodes/335-deploying-to-a-vps
My issue is: the default application is the one I deployed first, so when I visit the IP address, that is the app which is displayed. How do I configure the server to
Use a subdomain (not sure this is possible using just an IP address)
Change the default app
Had a play around in nginx.conf and read this stack q: NGinx Default public www location?
I can't seem to work it out! Thanks in advance.
I think I understand what you want to do. Your default app term confuses me. Let's throw that out and just say you want to deploy two different rails apps to different domains-- sub or TLD, it doesn't matter. Also, I think you are wanting to deploy them to the same VPS server. Ryan's screencast doesn't include how to do this.
What you are probably looking for is how to host multiple sites (and rails apps) with nginx. Like Ryan's screencast, there are many steps involved to get everything working. I recommend you first focus on domain setup (DNS), then nginx setup. Leaving serving your rails app with unicorn for last.
First
Setup your domain and subdomain to point to the VPS. One way is to create DNS A records point to your VPS IP.
Second
Configure nginx to serve both sites. To get you started in the right direction I recommend you read this: multiple websites on nginx & sites-available. It sounds like you already have nginx serving your app on your domain. So steps might be like:
$ cd /etc/nginx/sites-available/
$ cp default subdomain.example.com
Edit subdomain.example.com accordingly. See nginx docs for details. Also, make sure /sites-available/default and /sites-available/subdomain.example.com are not using _ as server_name directive. Set them to their respective domain names. Also, for now point the root to somewhere that will serve an index.html file (ie. leave rails out of it for now)
$ cd /etc/nginx/sites-enabled/
$ ln -s ../sites-available/eden.jrutherford.com .
$ service nginx restart
If all is well by this point you should be able to visit both domains in a browser and have nginx serve content.
Third
Configure a new unicorn for your subdomain. I'm sorry I don't have specific tips for this step . Follow Ryan's tutorial, search google, unicorn website.
Good Luck.

Deploying another rails app to a sub uri with unicorn and nginx

I have rails app running on unicorn+nginx. Now i wanted to deploy another small sinatra app sub uri(localhost:3000/test). Same requirement i achieved on passenger+nginx combination.
Deploying a rails app to a sub uri with passenger and nginx
Any suggestion will be appropriated.
I am assuming that you already know hoe to setup Ngnix with one Unicorn.
There are now fundamentally two ways you can do what you need.
Approach 1
Run another Unicorn (different folder, different port). For the URL pattern, setup another location in Nginx and set proxy directive to this Unicorn instance.
Approach 2
If you want to run both of these application in the same Ruby process, Rack can be used to send a set of URL patterns to Sinatra Application and rest to your Rails Application. If you are using Rails 3, you can do this in your routes.rb as well.
Please let me know if you need sample code for any of these approaches.

Add a reverse proxy to heroku

I have a rails app running on heroku at, e.g myapp.herokuapp.com.
Now I want to reverse proxy from myapp.heroku.com/proxy/ to somewhereelse.com/ (i.e: myapp.heroku.com/proxy/stuff is reverse proxifed to somewhereelse.com/stuff)
Is that possible on Heroku? How to achieve this?
For anyone coming to this question through a search, this can be done.
Check out https://github.com/ryandotsmith/nginx-buildpack to vendor nginx into your heroku instance. This will place nginx in front of your rails app, and allow you to reverse proxy requests on this domain, having your heroku app configured as apex and allowing somewhereelse.com/stuff go elsewhere.
You dont have access to frontend routing infrastructure so its not possible to do add something like nginx location based reverse proxying or apache's modproxy. From my understanding too you can only bind to one port (the $PORT) within the dyno so its not possible to shadow your Rails app with your own vendored version of nginx (unless it is possible to communicate over a non TCP/IP socket between nginx and your rack/rails app, if this is the case then perhaps you can get rack to listen to /tmp/mysocket.git and nginx to reverse proxy on this, this could be a no starter though, Im just throwing out ideas).
Which means the only probable option if you have to handle this yourself in your rails app, I have only a tiny tiny bit of rails/ruby experience, but if no proxy functionality exists in rails then you perhaps you can explicitly accept the route and then use a http client to invoke the other parts.

Resources