Setup nginx + flask load balancer

nginx config

upstream backend{server 127.0.0.1:90;server 127.0.0.1:91;}server {listen 81 default_server;listen [::]:81 default_server;location / {proxy_pass http://backend;}}

flask sample server to test

s1.py (simulate server 1)

from flask import Flaskapp = Flask(__name__)@app.route('/')def helloIndex():    return 'Hello World from server1'app.run(host='0.0.0.0', port=90)

s2.py (simulate server 2)

from flask import Flaskapp = Flask(__name__)@app.route('/')def helloIndex():    return 'Hello World from server2'app.run(host='0.0.0.0', port=91)

The fix of issue : connect() to 127.0.0.1:90 failed (13: Permission denied) while connecting to upstream :

> setsebool httpd_can_network_connect on

result :

default balance strategy is using weight, and each server weight=1 . so half request will be routed to server1 and another half to server2 .

so if change nginx config as below .

upstream backend{server 127.0.0.1:90 weight=2;server 127.0.0.1:91 weight=1;}

2/3 request will be routed to port 90 and 1/3 to port 91. results like blow

other balancing stategies

least_conn: As the name tells, directs the requests to the server with the least active connections at that time.

ip_hash: IP hashing uses the visitors IP address as a key to determine which host should be selected to service the request. This allows the visitors to be each time directed to the same server(unless it is down)

That’s all .

Thanks for reading !

--

--

--

A Senior Software Developer/Body builder . to help others enjoy coding and stay healthy

Love podcasts or audiobooks? Learn on the go with our new app.

Installing & Configuring OpenLink’s Enterprise Edition (Multi-Tier) Data Access for JDBC Data…

Contain apply reality line resource.

Para 🎲🎲 Lost File# 18

Making The Entire User Analytics Solution — Part I

Getting into Toptal as a Software tester

Waterbear Cloud Newsletter, September 2019

Stress Testing Bots in Azure Bot Service using Apache JMeter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
LORY

LORY

A Senior Software Developer/Body builder . to help others enjoy coding and stay healthy

More from Medium

Copying Django Model Objects with foreign keys Efficiently with Postgres

Sorting Algorithms: Quick Sort in Golang

How to apply Django migrations in Kubernetes GKE cluster

Celery: Django Admin Action to Manually Retry Tasks

Django Admin Interface with Retry Action (uses Django-celery-results)