How to achieve load balance in Nginx(Tengine)
pre-requisite:
- at least 2 linux nodes installed with Tengine, instruction
I will use 2 linux nodes in this doc
- linux node1, ip: 192.168.157.11
- linux node2, ip: 192.168.157.12
Steps
in this doc I will use Tomcat to serve page
- download tomcat tar, I use Tomcat-8 in this doc, link
- upload the tar file into all linux nodes
- in each nodes, execute following commands
tar -zxvf apache.xx.tar.gz -C /usr/local
cd /usr/local/{your apache folder}/webapps/ROOT
vi index.jsp
do some change so that we can distinguish different nodescd ../../bin
./catlina.sh run
- open browser, open
{ip}:8080
to make sure tomcat is running
- in main linux node, in my case, node01
vi /usr/local/tengine/conf/nginx.conf
- before server keyword, add following
-
upstream tomcats { server 192.168.157.11:8080; server 192.168.157.12:8080; }
-
- in server keyword, replace block of
location /
to-
location / { proxy_pass http://tomcats; }
-
- run
service nginx reload
- in browser, open
192.168.157.11
, refresh the page, should see the page from different nodes.
- the final nginx.conf for reference.
FAQ
- how to check the error message of Tengine
tail -f /usr/local/tengine/logs/error.log