Elastic loadbalancing — Gunicorn + Nginx + AWS ELB + Flask-socketio

Jupyter Data
2 min readMar 22, 2016

--

For the following setup, Flask-socketio has to be in 0.6.0 with the following dependencies

gevent==1.0.2
gevent-socketio==0.3.6
gevent-websocket==0.9.5
greenlet==0.4.9
gunicorn==18.0

shortcut for installation of the proper version :

>> sudo pip install flask-socketio==0.6.0 && sudo pip install gevent==1.0.2

Make sure that socketio js is at 0.9.16: https://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js

From the AWS console: set up your loadbalancer:

Make sure to set TCP port 80 (loadbalancer) forwarded to Application port 80 (Note: also open up port 80 on your security group)

Make sure to set To set the health check with TCP Protocol as well

check AWSCLI version and update to the latest with a pip install — upgrade awscli

>> AWS Configure

Create Loadbalancer policy: (http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/enable-proxy-protocol.html)

>> aws elb create-load-balancer-policy --load-balancer-name my-loadbalancer --policy-name my-ProxyProtocol-policy --policy-type-name ProxyProtocolPolicyType --policy-attributes AttributeName=ProxyProtocol,AttributeValue=true

Set loadbalancer policy:

>> aws elb set-load-balancer-policies-for-backend-server --load-balancer-name my-loadbalancer --instance-port 80 --policy-names "[]"

Check if it worked properly:

>> aws elb describe-load-balancers --load-balancer-name my-loadbalancer

Enable Proxy protocol on Nginx: (Listens on port 80 and forwards it onto application at port 8000 where we have bound a gunicorn instance)

Useful Nginx conf link: https://www.linode.com/docs/websites/nginx/how-to-configure-nginx

server { 
listen 80 proxy_protocol;

server_name localhost;

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_pass
http://localhost:8000/;
proxy_redirect off;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “Upgrade”;
}
}

Run gunicorn at port 8000 as mentioned above

>> sudo gunicorn -b 0.0.0.0:8000 — worker-class socketio.sgunicorn.GeventSocketIOWorker -w 1 -e yourenvironment_variables run:app

Should you need any Data Analysis/Visualization — Do checkout JetPack Data: Fastest way to visualize your Excel/ CSV files !

Fastest Way to Visualize Your Data

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jupyter Data
Jupyter Data

Written by Jupyter Data

Fastest way to Explore your Data

No responses yet

Write a response