您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)nginx+中怎么實(shí)現(xiàn)集群負(fù)載均衡,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
一,配置nginx
安裝nginx包
a.安裝pcre
tar zxvf pcre-7.2.tar.gz
cd pcre
./configure --prefix = /pcre
Make;make install
b,安裝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, 修改配置文件
Vi /nginx/conf/nginx.conf
#用戶組
user nobody nobody;
#cpu個(gè)數(shù),可以按照實(shí)際服務(wù)器來計(jì)算
worker_processes 8;
worker_rlimit_nofile 51200;
events {
use epoll;
#連接數(shù)
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;
#緩存的時(shí)間,(可以根據(jù)不同文件設(shè)置不同時(shí)間)
# 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;
#設(shè)定負(fù)載均衡列表
upstream backend
{
server 172.23.254.2:8080;
server 172.23.254.3:8080;
}
#設(shè)定虛擬主機(jī)
server {
listen 80;
server_name www.abc.com;
#對(duì) / 所有做負(fù)載均衡(本機(jī)nginx采用完全轉(zhuǎn)發(fā),所有請(qǐng)求都轉(zhuǎn)發(fā)到后端的tomcat集群)
location / {
root /web/www ;
index index.jsp index.htm index.html;
proxy_redirect off;
#保留用戶真實(shí)信息
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;
}
}
}
主要在配置proxy與upstream
Upstream具有負(fù)載均衡能力,可以自動(dòng)判斷下面的機(jī)器,并且自動(dòng)踢出不能正常提供服務(wù)的機(jī)器。
4,啟動(dòng)程序
/nginx/sbin/nginx
5,編寫啟動(dòng)腳本
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
二,配置tomcat
1, 下載tomcat5.59
tar zxvf tomcat5.59
2,修改配置文件
a,配置數(shù)據(jù)源
b,優(yōu)化tomcat最大并發(fā)數(shù)
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="2048" minSpareThreads="100" maxSpareThreads="200"
enableLookups="false" redirectPort="8443" acceptCount="500"
connectionTimeout="20000" disableUploadTimeout="true" />
c,添加虛擬主機(jī)
(注,主轉(zhuǎn)發(fā)的虛擬主機(jī)必須用localhost,否則nginx不能通過內(nèi)網(wǎng)ip轉(zhuǎn)發(fā),而只有通過域名轉(zhuǎn)發(fā)
d,測(cè)試
打開http://ip:8080
頁面能訪問則正常
2, 其他的tomcat服務(wù)器也用同樣的配置
三,做tomcat集群
兩臺(tái)機(jī)器 172.23.254.2 172.23.254.3
做集群需要修改的文件配置有三個(gè)地方
1,修改conf/server.xml配置文件
a. 找到Engine標(biāo)簽,加入屬性 jvmRoute="worker1"
b.找到Cluster標(biāo)簽,去掉注釋,同時(shí)修改tcpListenAddress為本機(jī)ip 172.23.254.2 (注:這一段Cluster必須放在hosts里面)
2, 修改應(yīng)用的web.xml
修改web應(yīng)用里面WEB-INF目錄下的web.xml文件,加入標(biāo)簽
<distributable/>
直接加在</web-app>之前就可以了
這個(gè)是加入tomcat的session復(fù)制的,做tomcat集群必須需要這一步,否則用戶的session就無法正常使用。
3, 開啟防火墻
這兩個(gè)tomcat之間必須開啟防火墻信任。
分別啟動(dòng)兩個(gè)tomcat,查看每一個(gè)tomcat是否都啟動(dòng)了8080端口以及4001端口
再用netstat –an 查看鏈接情況
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
如果兩臺(tái)機(jī)器的4001端口分別建立了連接,則說明集群配置成功,可以進(jìn)行session復(fù)制。
關(guān)于nginx+中怎么實(shí)現(xiàn)集群負(fù)載均衡就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。