在反向代理的配置中,nginx的服务器负载均衡存在很多优势,不少企业都用这种软件环境进行业务流程。那么现在就来为大家介绍一下nginx负载均衡器的软件配置过程。包含的内容比较多,那么大家可以仔细浏览一下。
Nginx负载均衡器的优点许多,简单概括为:
①实现了可弹性化的架构,在压力增大的时候可以临时添加tomcat服务器添加到这个架构里面去;
②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器;而Keepalvied可保证单个nginx负载均衡器的有效性,避免单点故障.
IP地址
nginx(主负载均衡器):192.168.1.106 nginx(从负载均衡器):192.168.1.107 VIP地址:192.168.1.108 tomcat1的IP:192.168.1.102 tomcat2的IP:192.168.1.103 tomcat3的IP:192.168.1.105
布置整个环境用到的软件为:
apache-tomcat-6.0.20.tar.gz jdk-6u16-linux-i586.bin nginx-0.7.17.tar.gz prce-7.7.tar.gz
①首先分别在3台tomcat主机上布置java环境,安装tomcat,具体步骤为:
安装java环境:
JDK的安装 #chmod+xjdk-6u16-linux-i586.bin #./jdk-6u16-linux-i586.bin 到此JDK已经安装完成 建立符号链接 #ln-s/usr/local/jdk-1.6.0-16/bin/java/usr/bin/java #ln-s/usr/local/jdk-1.6.0-16/bin/javac/usr/bin/javac ln-s/usr/local/jdk-1.6.0-16/usr/local/jdk ln-s/usr/local/jdk-1.6.0-16/jre/usr/local/jre TOMCAT的安装 #tarzxvfapache-tomcat-6.0.20.tar.gz #mvapache-tomcat-6.0.20/usr/local/tomcat
设置环境变量: vim /etc/rc.d/rc.local,以追加的形式添加如下内容
JAVA_HOME=/usr/local/jdk exportJAVA_HOME JRE_HOME=/usr/local/jre exportJRE_HOME CLASSPATH=/usr/local/tomcat/common/lib/:/usr/local/jdk/lib:/usr/local/jre/lib exportCLASSPATH PATH=$PATH:/usr/local/tomcat/bin/:/usr/local/apache/bin:/usr/local/jdk/bin:/usr/local/jre/bin exportPATH TOMCAT_HOME=/usr/local/tomcat exportTOMCAT_HOME 重启服务器,启动tomcat以检查是否存在错误: #cd/usr/local/tomcat/bin #./startup.sh http://IP地址:8080可以看到猫头:)成功了。
#p#②分别在二台nginx负载均衡器上安装nginx及配置
下载及安装nginx:
wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz tarzxvfpcre-7.7.tar.gz cdpcre-7.7/ ./configure make&&makeinstall wgethttp://sysoev.ru/nginx/nginx-0.7.17.tar.gz tarzxvfnginx-0.7.17.tar.gz cdnginx-0.7.17/ ./configure--prefix=/usr/local/nginx--with-http_stub_status_module make&&makeinstall
这里贴上nginx.conf配置文件,不加任何说明,方便用X-shell直接粘贴,很人性化的噢:)二个nginx负载均衡器的文件一样,配置完成后分别用/usr/local/nginx/sbin/nginx启动。
usernobodynobody; worker_processes1; pid/usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile51200; events { useepoll; worker_connections51200; } http{ includemime.types; default_typeapplication/octet-stream; server_names_hash_bucket_size128; client_header_buffer_size32k; large_client_header_buffers432k; client_max_body_size8m; sendfileon; tcp_nopushon; keepalive_timeout60; tcp_nodelayon; fastcgi_connect_timeout300; fastcgi_send_timeout300; fastcgi_read_timeout300; fastcgi_buffer_size64k; fastcgi_buffers464k; fastcgi_busy_buffers_size128k; fastcgi_temp_file_write_size128k; gzipon; gzip_min_length1k; gzip_buffers416k; gzip_http_version1.0; gzip_comp_level2; gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml; gzip_varyon; upstreambackend { server192.168.1.102:8080; server192.168.1.103:8080; server192.168.1.105:8080; } server{ listen80; server_namewww.yuhongchun027.com; location/{ root/var/www; indexindex.jspindex.htmindex.html; proxy_redirectoff; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://backend; } #location/nginx{ #access_logon; #auth_basic"NginxStatus"; #auth_basic_user_file/usr/local/nginx/htpasswd; #} log_formataccess'$remote_addr-$remote_user[$time_local]"$request"' '$status$body_bytes_sent"$http_referer"' '"$http_user_agent"$http_x_forwarded_for'; access_log/var/log/access.logaccess; } }
#p#③在二台Nginx机上安装及配置keepalived,做双机互备。
#wgethttp://www.keepalived.org/software/keepalived-1.1.15.tar.gz #tarzxvfkeepalived-1.1.15.tar.gz #cdkeepalived-1.1.15 #./configure #make #makeinstall 将keepalived做成启动脚务,方便管理: #cp/usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/ #cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/ #mkdir/etc/keepalived #cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/ #cp/usr/local/sbin/keepalived/usr/sbin/ #servicekeepalivedstart|stop
在二台Nginx上,我分别贴出keepalived.conf配置文件,配置完成后分别用service keepalived start启动。检验keepalived是否成功可用命令
ipavrrp_instanceVI_INET1{ stateMASTER interfaceeth0 virtual_router_id53 priority200 advert_int1 authentication{ auth_typepass auth_passyourpass } virtual_ipaddress{ 192.168.1.108 } } vrrp_instanceVI_INET1{ stateBACKUP interfaceeth0 virtual_router_id53 priority100 advert_int1 authentication{ auth_typepass auth_passyourpass } virtual_ipaddress{ 192.168.1.108 } }
以上三步完成nginx负载均衡设置后,就是检测了,大家可任意停掉其中一台的任何服务;在正常结果中大家应该发现,其中任意一台服务器宕机后,不影响整个系统的运作。
转载请注明:IT运维空间 » 运维技术 » 三步安装完成nginx负载均衡设置
发表评论