load balancing different tornado apps on nginx

695 views
Skip to first unread message

L-R

unread,
Oct 1, 2012, 11:24:49 AM10/1/12
to python-...@googlegroups.com
I've found the config on the Tornado page to load balance one app running on different ports (ex: 8000, 8001, 8002, 8003) and that works pretty well. But how about having a first app that is configured to run on http://myserver/first_url and then a second at http://myserver/second_url, mapped to different ports? If I build the apps and run them on ports 8000 and 8001, and then enumerate my frontends in ngxing

upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
}

This will of course load balance the two apps separately, and give me a 404 half of the time, which is normal. Would this need to be configured in the "location" section of my "server" declaration of nginx?

Note : am using supervisor to run the app(s)

Thanks.

Didip Kerabat

unread,
Oct 1, 2012, 11:56:00 AM10/1/12
to python-...@googlegroups.com
Yeah, you definitely need to setup 2 location blocks.

1. location = /first_url { proxy_pass frontend_first_url }
2. location = /second_url { proxy_pass frontend_second_url }

# first app
upstream frontend_first_url {
server 127.0.0.1:8000
}

# second app
upstream frontend_second_url {
server 127.0.0.1:8001
}

Something like that, above is just pseudocode.

- Didip -

L-R

unread,
Oct 1, 2012, 12:42:55 PM10/1/12
to python-...@googlegroups.com
hey Didip,
thanks for the tip - it actually worked like a charm at the first try.

For reference:

[ ... ]

    # two Tornado servers running the same app / nginx load balancing them
    upstream frontends_first_app {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
    }

    # one Tornado server running another different app
    upstream frontends_second_app{
        server 127.0.0.1:8002;
    }

[ ... ]

        #main app
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends_first_app;
        }

        #second app, running at a different URL
        location /specialurl {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends_second_app;
        }

Cheers!
Reply all
Reply to author
Forward
0 new messages