Nginx and Hypnotoad proxy correct config

71 views
Skip to first unread message

StarX

unread,
Feb 14, 2017, 3:13:58 PM2/14/17
to Mojolicious
Hi,

I know this has been asked many times and there are various posts addressing this but I'm struggling to find a concrete example, maybe my approach is wrong but I'd appreciate some help.

So, I have a number of mojo applications, say app1, app2, appN, they all work great when I spin them up with hypnotoad on their own ports, app1 runs on 5000, app2 on 5001, appN 5..N.  Can I put all these separate apps behind Nginx and have Nginx serve the js/css content from the public folder for each app on a single vhost? I have spent days trying to work this out so I may be on completely the wrong track. 

Here is my Nginx conf on my dev box for app1 and app2.  I can't seem to get Nginx to serve the static content from the public directory for each app, it only works if I proxy all requests.  I know this is more of a Nginx question than Mojo specific but can anyone help please?  What is the recommended way to do this?

server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        # Root for stuff like default index.html
        root /var/www/html;

      #  location ~* /(images|css|js|files)/ {
      #     root /home/username/app1/public/;
      #    expires 7d;
      #  }

        location /app1 {
           proxy_pass http://127.0.0.1:5000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "upgrade";
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Forwarded-Host $host:$server_port;
       }

        location /app2 {
           proxy_pass http://127.0.0.1:5001;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection "upgrade";
           proxy_set_header Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Forwarded-Host $host:$server_port;
       }

         location /appN {...}
}

I've also looked at Toadfarm to combine my apps into one but I'm concerned about scalability, doesn't Toadfarm check each route until it finds a match?  If I have 15 mojo apps running how is this going to impact on performance?

Thanks

Rob Willett

unread,
Feb 14, 2017, 3:31:53 PM2/14/17
to Mojolicious
See my previous reply to you on this. The config files is just pulled from a production server so we know it works :)

Rob

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscribe@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

StarX

unread,
Feb 15, 2017, 4:26:23 AM2/15/17
to Mojolicious
Hi Rob,

Thanks for you reply, sorry I thought I'd deleted that original post.

As I explain below I'm trying to acheive the following:

myserver.com/app1 proxied to hypnotoad listening on http://*:5000
myserver.com/app2 proxied to hypnotoad listening on http://*:5001
myserver.com/appN proxied to hypnotoad listening on http://*:5..N
Thanks for you reply, sorry I thought I'd deleted that original post.  In the example that you kindly provided you're proxying all requests (/) right?

As I mentioned below I'm trying to achieve the following:

myserver.com/app1 dynamic requests proxied to hypnotoad listening on http://*:5000, css/js files served directly by Nginx (example path app1/public/js/script.js)
myserver.com/app2 dynamic requests proxied to hypnotoad listening on http://*:5001, css/js files served directly by Nginx (example path app2/public/js/script.js)
myserver.com/appN dynamic requests proxied to hypnotoad listening on http://*:5..N, css/js files served directly by Nginx (example path app3/public/js/script.js)

I'm adding location directives to my Nginx conf as shown in my second post (below) but I've seen a lot of posts about serving the static files for each app (js/css) directly from Nginx - although all the examples I’ve seen are only based around a single app rather than app1, app2 etc within the same server block, I hope that makes sense.

Am I going about this the right way?  If so can I get Nginx to to serve the public content for each app directly.

Thanks again for your help.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.

Rob Willett

unread,
Feb 16, 2017, 1:59:02 AM2/16/17
to mojol...@googlegroups.com
I'm not really sure what your problem is. 

I can see that you have different apps running on different ports. So far so good. 

This (to me) look ok and should work. 

We put different apps in different ports, Nginx simply serves the apps out. No big deal. The config file has this laid out. 

What actually doesn't work?

Heiko Jansen

unread,
Feb 16, 2017, 3:30:20 AM2/16/17
to Mojolicious
Did not try to verify this, but if I understand your example config correctly it may be related to the way nginx processes "location" directives.

nginx first searches for the most specific prefix location given by literal strings regardless of the listed order. In the configuration above the only prefix location is “/” and since it matches any request it will be used as a last resort. Then nginx checks locations given by regular expression in the order listed in the configuration file. The first matching expression stops the search and nginx will use this location. If no regular expression matches a request, then nginx uses the most specific prefix location found earlier. 

HTH
- Heiko 
Reply all
Reply to author
Forward
0 new messages