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 !