? 目的:本次使用了三台虚拟机,实现了Nginx+tomcat的负载均衡控制,nginx作为转发控制器,tomcat作为后端控制,这样做的好处,避免直接暴漏tomcat服务器给公网,可以实现负载均衡和错误迁移功能,确保服务的高可用性
server 192.168.1.113:80 安装nginx作为负载均衡的转发器
server 192.168.1.106:8080 安装tomcat
server 192.168.1.109:8080 安装tomcat
?
步骤一、安装配置tomcat,分别在两台机器上面,启动tomcat
[root@master ~]# tar -zxvf apache-tomcat-6.0.44.tar.gz
[root@master ~]# mv apache-tomcat-6.0.44 tomcat
#为了区分不同的后端服务器建议,在每个tomcat的Root目录中,修改index.html文件,便于区分不同的后端机器
[root@master bin]# ./startup.sh
步骤二、安装配置Nginx
[root@node1 conf]# cat nginx.conf
user? nobody;#
worker_processes? 2;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid??????? logs/nginx.pid;
events {
??? use epoll;#Linux
??? worker_connections? 2048;
?? }
http {
??? include?????? mime.types;
??? default_type? application/octet-stream;
??? #log_format? main? ‘$remote_addr - $remote_user [$time_local] $request ‘
??? #????????????????? ‘"$status" $body_bytes_sent "$http_referer" ‘
??? #????????????????? ‘"$http_user_agent" "$http_x_forwarded_for"‘;
??? #access_log? off;
??? access_log? logs/access.log;#
??? sendfile??????? on;
??? #tcp_nopush???? on;
??? tcp_nodelay???? on;
??? keepalive_timeout? 65;
??? #include???? gzip.conf;
??? upstream tomcats {
???????? server 192.168.1.106:8080? weight=2;
???????? server 192.168.1.109:8080? weight=2;
??? }
??? server {
??????? listen?????? 80;#HTTP
??????? server_name? localhost;
??????? charset utf-8;
??????? #access_log? logs/host.access.log? main;
??????? location / {
??????????? proxy_pass http://tomcats;#
??????????? #include proxy.conf;
??????? }
??????? error_page 404 /html/404.html;?
??? }
?}
启动nginx服务器
[root@node1 conf]# /usr/local/nginx/sbin/nginx
?
步骤三、测试高可靠性
? 使用 ps -ef |grep java 杀掉其中一个tomcat,继续访问http://192.168.1.113/
? 仍能访问,说明高可靠性成功。
原文:http://gaojingsong.iteye.com/blog/2267299