Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Cluster load balancing in nginx+

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve cluster Load Balancer in nginx+. The content of the article is of high quality, so Xiaobian shares it with you for reference. I hope you have a certain understanding of relevant knowledge after reading this article.

First, configure nginx

Install nginx package

a. installing pcre

tar zxvf pcre-7.2.tar.gz

cd pcre

./ configure --prefix = /pcre

Make;make install

b, install nginx

tar zxvf nginx-0.6.32.tar.gz

cd nginx-0.6.32

./ configure --prefix=/nginx -with-pcre=/pcre --with-http_rewrite_module

Make;make install

3. Modify the configuration file

Vi /nginx/conf/nginx.conf

#User Groups

user nobody nobody;

#CPU number, can be calculated according to the actual server

worker_processes 8;

worker_rlimit_nofile 51200;

events {

use epoll;

#Number of connections

worker_connections 8192 ;

}

http {

include mime.types;

default_type application/octet-stream;

server_names_hash_bucket_size 128;

# access_log off;

# access_log logs/access.log;

#Time of cache,(different times can be set according to different files)

# expires 2h;

tcp_nodelay on;

keepalive_timeout 30;

gzip on;

gzip_min_length 10;

gzip_buffers 4 8k;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/html application/xml;

sendfile on;

tcp_nopush on;

reset_timedout_connection on;

client_max_body_size 30m;

#Set Load Balancer List

upstream backend

{

server 172.23.254.2:8080;

server 172.23.254.3:8080;

}

#Setting up virtual hosts

server {

listen 80;

server_name www.abc.com;

#Load Balancer for/all (native nginx uses full forwarding, all requests are forwarded to the tomcat cluster on the backend)

location / {

root /web/www ;

index index.jsp index.htm index.html;

proxy_redirect off;

#Keep real user information

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://backend;

}

}

}

Mainly in configuring proxy and upstream

Upstream has the ability of Load Balancer, which can automatically determine the machines below and automatically kick out the machines that cannot provide service normally.

4. Start the program

/nginx/sbin/nginx

5. Write a startup script

Vi nginx.sh

#!/ bin/sh

CWD=`pwd`

case $1 in

start)

/nginx/sbin/nginx;

;;

stop)

kill -2 `ps -ef|grep "/nginx/sbin/nginx"|grep -v "grep"|awk '{print $2}' `

;;

restart)

cd "$CMD"

$0 stop

$0 start

;;

*)

echo $"Usage: $0 {start|stop|restart}"

exit 1

esac

exit 0

Second, configure tomcat

Download Tomcat 5.59

tar zxvf tomcat5.59

2. Modify the configuration file

a. Configure data sources

b, optimize tomcat maximum concurrency

c. Add virtual host

(Note, the virtual host of the main forwarding must use localhost, otherwise nginx cannot be forwarded through the intranet ip, but only through the domain name.)

d, testing

Open http://ip:8080

The page can be accessed normally

Other tomcat servers use the same configuration.

Third, do tomcat cluster

Two machines 172.23.254.2 172.23.254.3

There are three places to modify the file configuration for clustering

1. Modify the conf/server.xml configuration file

a. Find the Engine tag and add the attribute jvmRoute="worker1"

b. Find the Cluster label, remove the comment, and modify tcpListenAddress to be the ip 172.23.254.2 (Note: This section of Cluster must be placed inside hosts)

2. Modify the web.xml of the application

Modify the web.xml file under WEB-INF directory in web application and add tag

Just add it directly to the front

This is to join tomcat session replication, do tomcat cluster must need this step, otherwise the user's session will not be able to use normally.

3. Open the firewall

Firewall trust must be enabled between these two tomcats.

Start two tomcats separately and check whether each tomcat starts port 8080 and port 4001.

Then use netstat -an to view links

tcp 0 0 172.23.254.2:43320 172.23.254.3:4001 ESTABLISHED

tcp 0 0 172.23.254.2:46544 172.23.254.3:4001 TIME_WAIT

tcp 0 0 172.23.254.2:40118 172.23.254.3:4001 ESTABLISHED

tcp 0 0 172.23.254.2:4001 172.23.254.3:48804 ESTABLISHED

tcp 0 0 172.23.254.2:4001 172.23.254.3:34254 ESTABLISHED

If port 4001 of the two machines is connected separately, the cluster configuration is successful and session replication can be performed.

How to achieve cluster Load Balancer in nginx+ is shared here. I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report