Request for help in configuring Nginx with Unicorn and two or more Rails apps

14 views
Skip to first unread message

Thomas Elam

unread,
Apr 14, 2017, 3:31:26 AM4/14/17
to bangal...@googlegroups.com
Hi, folks. Since this is a nice, friendly crowd, I was wondering whether anyone here has tried using Nginx server blocks for multiple Rails apps to keep the recurring costs of VPS down and would be willing to share an example configuration. I've already got a working configuration for one Rails app on Nginx and Unicorn.

I've been reading tutorials on DigitalOcean.com (my host) and Nginx.org, but I'd like to get something running before learning all the ins and outs.

Please send replies to this group and also directly to me (so I won't miss them).

Thanks.

Tom

Thomas Elam

unread,
Apr 14, 2017, 9:36:31 AM4/14/17
to bangal...@googlegroups.com
I'm forwarding some of Anand's help and the parts I figured out, in case it will help other people.

Tom

---------- Forwarded message ----------
From: Anand Chitipothu <xx...@xxxx.com>
Date: Fri, Apr 14, 2017 at 1:40 PM
Subject: Re: [Bangalore RUG] Request for help in configuring Nginx with Unicorn and two or more Rails apps
To: Thomas Elam <tom...@gmail.com>



Usually people keep one config file per domain. So you may want to have two files zwm.org.in.conf and mashweb.club.conf

Replace the "server_name _;" with the domain name you want to serve. Restart nginx after you are done with those changes.

Which linux distribution are you using on your server? That'll give me an idea of how exactly the files will be organized.

If you can send me the output of "tree /etc/nginx" that will give me a better idea.

Anand


On Fri, Apr 14, 2017 at 1:33 PM, Thomas Elam <tom...@gmail.com> wrote:
Thanks, Anand.

Here's my sites-enabled file (with filename mashweb), which directs requests to my mashwebclub app properly. I tried adding another server block but couldn't get it to point to the other Rails app. Previously the same file except with the difference that server_name was mashweb.club also worked. My other domain, zwm.org.in, gets directed to the 'Welcome to Nginx!' page. (I was really surprised by that, since server_name is now _.) Each Rails app has its own root, socket, and domain name. Both domain names point to the droplet's IP address.

upstream mashwebclub_app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/home/rails/mashwebclub/shared/sockets/unicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name _;

    try_files $uri/index.html $uri @mashwebclub_app;

    root /home/rails/mashwebclub/public;

    location @mashwebclub_app {
        proxy_pass http://mashwebclub_app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

Tom

On Fri, Apr 14, 2017 at 1:12 PM, Anand Chitipothu <anand...@gmail.com> wrote:
Hi Tom,

I run lot of webapps on my digital ocean. I don't have experience with rails, but if you can share an example of working configuration, I'll be able to help you with the rest.

Anand


--
You received this message because you are subscribed to the Google Groups "BANGALORE RUG-Ruby Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bangalorerug+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.




Thomas Elam

unread,
Apr 14, 2017, 9:39:37 AM4/14/17
to bangal...@googlegroups.com
I already knew about the use of the sites-enabled and sites-available, but I'm forwarding it to the group for completeness.

Tom

---------- Forwarded message ----------
From: Anand Chitipothu <anand...@gmail.com>
Date: Fri, Apr 14, 2017 at 2:22 PM
Subject: Re: [Bangalore RUG] Request for help in configuring Nginx with Unicorn and two or more Rails apps
To: Thomas Elam <tom...@gmail.com>



Ok, the trick is sites-enabled. Only the config files in sites-enabled/ will be loaded. If you want to enable a config file, you need to add it to sites-enabled. The usual practice is to keep configs of all the sites in the sites-available and the enabled ones in sites-enabled, usally by creating a symlink to the config file in sites-available.

So, in your case you need to symlink the config file for the second domain to sites-enabled.

Anand

On Fri, Apr 14, 2017 at 2:10 PM, Thomas Elam <tom...@gmail.com> wrote:
I'm using the Rails One-Click App', which has Ubuntu 16.04. A fuller description is at https://goo.gl/4tFuUO . I ripped out rvm and replaced it with rbenv. I tried one config file per domain, but maybe I messed it up. I don't suppose you're familiar with unicorn, but it's correctly listening on a socket in the one-app case.

Here's the output of tree:

root@ruby-rails-512mb-blr1-02:~# tree /etc/nginx
/etc/nginx
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── nginx.conf.orig
├── proxy_params
├── scgi_params
├── sites-available
│   ├── default
│   ├── default.orig
│   ├── mashwebclub
│   ├── mashwebclub.1domain-ok
│   ├── mashwebclub.2domains
│   ├── mashwebclub.mashwebclub
│   ├── mashwebclub.no
│   ├── rails
│   ├── rails.orig
│   └── zwm
├── sites-enabled
│   └── mashwebclub -> ../sites-available/mashwebclub
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

4 directories, 24 files

Tom


Tom

--

Thomas Elam

unread,
Apr 14, 2017, 9:39:50 AM4/14/17
to bangal...@googlegroups.com

Tom

---------- Forwarded message ----------
From: Anand Chitipothu <anand...@gmail.com>
Date: Fri, Apr 14, 2017 at 2:40 PM
Subject: Re: [Bangalore RUG] Request for help in configuring Nginx with Unicorn and two or more Rails apps
To: Thomas Elam <tom...@gmail.com>


On Fri, Apr 14, 2017 at 2:26 PM, Thomas Elam <tom...@gmail.com> wrote:
Yeah, I tried that. I really don't understand the basics. When are two server blocks in one config file appropriate? When are two config files appropriate?

For nginx, both of the look exactly the same. They split into multiple files just for ease of managing them.

If you see /etc/nginx/nginx.conf, you'll find the following two lines.

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

It is just including all the config files from sites-enabled. Thats all.
 
Sounds like I need to try the second config file again.
 
Let me know if that doesn't work. If the server refuses to start, then look at /var/log/nginx/error.log for error messages.

Anand

Thomas Elam

unread,
Apr 14, 2017, 9:40:10 AM4/14/17
to bangal...@googlegroups.com

Tom

---------- Forwarded message ----------
From: Anand Chitipothu <anand...@gmail.com>
Date: Fri, Apr 14, 2017 at 5:38 PM
Subject: Re: [Bangalore RUG] Request for help in configuring Nginx with Unicorn and two or more Rails apps
To: Thomas Elam <tom...@gmail.com>


On Fri, Apr 14, 2017 at 5:24 PM, Thomas Elam <tom...@gmail.com> wrote:
One more question (actually, a few), though.  Have you got any idea why mashweb.club comes up when I browse http://139.59.29.244 ?  It seems arbitrary.  Why not zwm.org.in at least sometimes?  Is there a priority?  Does it have to do with which service starts first?  Can I set a basic Nginx page if the domain name is not given?

I think it just picks the first one.

You can add a new configuration file "default".

server {
    listen   80;
    server_name _;
    root        /var/www/default;
}

And link it to sites-enabled.

Add a /var/www/default/index.html file with whatever you want to show on the default site.

Anand


Thomas Elam

unread,
Apr 14, 2017, 9:46:04 AM4/14/17
to bangal...@googlegroups.com
BRUG,

With a lot of help from Anand, I got this to work. This is going to be way better (read: much cheaper than Heroku and with no UpTimeRobot or StatusCake hacks to try to beat the sleeping dyno problem), especially if I can couple it with a free CloudFlare.com account like I did when the sites were on Heroku.com.

Tom

---------- Forwarded message ----------
From: Anand Chitipothu <anand...@gmail.com>
Date: Fri, Apr 14, 2017 at 5:41 PM
Subject: Re: [Bangalore RUG] Request for help in configuring Nginx with Unicorn and two or more Rails apps
To: Thomas Elam <tom...@gmail.com>


On Fri, Apr 14, 2017 at 4:07 PM, Thomas Elam <tom...@gmail.com> wrote:
WHOOPIE!  I got two Rails apps working on the same $5/month DigitalOcean droplet, using the same IP address but with different domain names!  You can see this for yourself (if you want) by running WHOIS on mashweb.club and zwm.org.in.  (Well, you'll see they're on the same IP address and that they both use DigitalOcean's name servers.)

There was a lot in my second app that wasn't set up, but I noticed this when I got the courage to try your suggestion.

Your service to me as an Nginx scout has been invaluable!

Thanks a lot, Anand. If I can ever help you with Rails, JavaScdript, Lisp, or Scheme, let me know.

Nice!

Not sure if you remember me, we had talked to each other in bangalore-fp mailing list and SICP study group IIRC. I'm not doing any scheme/FP these days though.

What are you up to?

Anand


Swanand Pagnis

unread,
Apr 16, 2017, 2:47:45 AM4/16/17
to bangal...@googlegroups.com
Thanks Tom!

This is exemplary user group behaviour!  Not a lot of people post a question, and then follow up with it, and share when they get an answer.  

Thomas Elam

unread,
Apr 18, 2017, 3:48:15 AM4/18/17
to bangal...@googlegroups.com
[The editing and sending of this message was delayed while my electrical power situation stabilized.]

Well, hi, Swanand.  I'm really glad you appreciate it.  Often I am very shy to ask questions in groups because I don't faithfully follow the groups I subscribe to, and I don't contribute many answers.  In this case, maybe Anand's and my conversation might encourage some people to buy several cheap domains and host servers on an ultra-cheap VPS.  Here are some tips:
  • The cheapest and easiest VPS/semi-cloud for getting started is probably DigitalOcean.com.
  • The sysops/sysadmin takes time unless it's automated.  People can clone my simple Makefile for this, read it, understand it, and adapt it.
  • Understanding Amazon Web Services (AWS) is very time-consuming.
  • If you're not careful with your secret keys, a bot on the Internet will steal them like they did mine.  (I probably accidentally uploaded them to my GitHub repository.  Idiot me!)  It's extremely scary to get a $10,000 bill you didn't expect!  Thankfully, Amazon refunded the money to me before the bill came due, after I deleted all my "instances" and whatnot.  It will be awhile before I tackle AWS again!
Anand was really great with his very prompt help and encouragement, even though I had to handle the Unicorn, Rails, and systemd aspects.  He prompted me to try some things I just didn't want to get into, and I was rewarded with success.

Tom
Reply all
Reply to author
Forward
0 new messages