最前面两台NGINX+keepalived下载可不可以同时承担WEB的高可用负载均衡和图片服务器并实现高可用?

Nginx+Keepalived高可用负载均衡双机热备_服务器应用_Linux公社-Linux系统门户网站
你好,游客
Nginx+Keepalived高可用负载均衡双机热备
来源:Linux社区&
作者:ruilinux
Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型。&Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接,作用:主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现&Nginx有很强代理功能,但是一台nginx就形成了单点,现在使用keepalived来解决这个问题,keepalived的故障转移时间很短.Nginx+keepalived双机实现nginx反向代理服务的高可用,一台nginx挂掉之后不影响应用也不影响内网访问外网。
一、环境:rhel5、nginx-0.7.51、keepalived-1.1.15master:192.168.126.173slave:192.168.126.176vip:192.168.200.16
&二:安装nginx(两台安装配置都一样!)&#添加运行nginx的用户和组www& groupadd www&& useradd -g www www wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz& tar zxvf pcre-7.8.tar.gz& cd pcre-7.8/& ./configure& make && make install& wget http://sysoev.ru/nginx/nginx-0.7.51.tar.gz& tar zxvf nginx-0.7.51.tar.gz& cd nginx-0.7.51/& ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module& make && make install
三:安装keepalived
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz& tar zxvf keepalived-1.1.15.tar.gz& cd keepalived-1.1.15& ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux(这里为什么做软链接后面说!) ./configure --prefix=/usr --exec-prefix=/usr \--bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share \--includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec \--localstatedir=/var --mandir=/usr/share/man --infodir=/usr/share/info \--sharedstatedir=/usr/com \--with-kernel-dir=/usr/src/linux(这里指定的是软链接路径!!) make make install 启动nginx!/usr/local/nginx/sbin/nginx
相关资讯 & & &
& (08月15日)
& (08月07日)
& (08月25日)
& (08月10日)
& (08月04日)
图片资讯 & & &
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款&Nginx+keepalived+tomcat实现tomcat高可用性负载均衡试验环境: CentOS5.4、pcre-8.12、nginx-utream-jvm-route-0.1、nginx-1.0.10、apache-tomcat-7.0.23 、keepalived-1.1.17.tar.gz、jdk-7u2-linux-x64.tar.gz主nginx服务器地址:10.29.9.200 辅nginx服务器地址:10.29.9.201 tomcat1: 10.29.9.202 tomcat2: 10.29.9.203VIP: 10.29.9.188拓扑如下:650) this.width=650;" title="绘图1" border="0" alt="绘图1" src="/d/file/p//f225c5b91afe9abc6224a.jpg" width="650" height="197" />&1.分别在10.29.9.200和10.29.9.201上面安装nginxtar zxf pcre-8.12.tar.gz cd pcre-8.12 ./configure make install&下载下面的插件安装,否则nginx无法识别tomcat中jvmRoute,从而无法达到session复制的效果。wget /LinuxSoft/nginx-utream-jvm-route-0.1.tar.gz tar xzf nginx-utream-jvm-route-0.1.tar.gz tar xzf nginx-1.0.10.tar.gz cd nginx-1.0.10 patch -p0 &../nginx_utream_jvm_route/jvm_route.patch ./configure --prefix=/usr/local/nginx --with-http_stub_status_module / --with-pcre=/root/pcre-8.12 --add-module=../nginx_utream_jvm_route/ #--with-pcre=指向的是pcre的源码包 make install2.配置nginxvim /usr/local/nginx/conf/nginx.confuser&worker_processes 4;error_log&/home/wwwlogs/nginx_error.log&pid /usr/local/nginx/logs/nginx.#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 51200;events&&&&&&&&& &&&&&&& {&&&&&& &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& worker_connections 51200;&&&&&&& }&&&&&&http&&&&&&& {&& utream backend { &&&&&&& server 10.29.9.202:8080 srun_id=tomcat1;&&&&&&& server 10.29.9.203:8080 srun_id=tomcat2;&&&&&&& jvm_route $cookie_JSESSIONID|&&&&&&& }include&&&&&& mime.default_type&application/octet-server_names_hash_bucket_size 128;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 50m;tcp_nopush&&&&keepalive_timeout 60;tcp_fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 256k;gzip_min_length&1k;gzip_buffers&&&& 4 16k;charset UTF-8gzip_http_version 1.0;gzip_comp_level 2;gzip_types&&&&&& text/plain application/x-javascript text/css application/gzip_#limit_zone&crawler&$binary_remote_addr&10m;&server{&&& listen&&&&&& 80; && server_name&; && index index.jsp index.htm index.&& root&/home/wwwroot/;location /&{&&&& proxy_pass&http://&&&& proxy_redirect&&& &&&& proxy_set_header&X-Forwarded-For&$proxy_add_x_forwarded_&&&& proxy_set_header&X-Real-IP&$remote_&&&& proxy_set_header&Host $http_&& &&} location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$&& &&&&&&&&{ &&&& &&&expires&&&&& 30d;&& &&} location ~ .*/.(js|css)?$&& { &&&& &&&expires&&&&& 1h;&& } location /Nginxstatus {&&& &&&stub_&& &&&&access_log&&&& } &log_format&access&'$remote_addr - $remote_user [$time_local] &$request& ''$status $body_bytes_sent &$http_referer& ''&$http_user_agent& $http_x_forwarded_for';access_log&/home/wwwlogs/access.log&&&&&&&& } include vhost/*.}&3. 分别在两台nginx服务器上安装keepalivedtar zxvf keepalived-1.1.17.tar.gzcd keepalived-1.1.17./configure --prefix=/usr/local/keepalivedmake && make installcp /usr/local/keepalived/sbin/keepalived /usr/sbin/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/mkdir /etc/keepalivedcd /etc/keepalived/&主keepalived配置vim keepalived.confvrrp_script chk_http_port {&&&&&&&&&&&&&&& script &/opt/nginx_pid.sh&&&&&&&&&&&&&&&& interval 2&&&&&&&&&&&&&&& weight 2}vrrp_instance VI_1 {&&&&&&& state MASTER&&&&&&& interface eth0&&&&&&& virtual_router_id 51&&&&&&& mcast_src_ip 10.29.9.200&&&&&&& priority 150&&&&&&& authentication {&&&&&&&&&&&&&&&&&&&&& auth_type PASS&&&&&&&&&&&&&&&&&& &&auth_pass 1111&&&&&&& }&&&&&&& track_script {&&&&&&&&&&&&&&& chk_http_port&&&&&&& }&&&&&&& virtual_ipaddress {&&&&&&&&&&&& 10.29.9.188&&&&&&& }}辅keepalived 配置vrrp_script chk_http_port {&&&&&&&&&&&&&&& script &/opt/nginx_pid.sh&&&&&&&&&& &&&&&&interval 2&&&&&&&&&&&&&&& weight 2}vrrp_instance VI_1 {&&&&&&& state BACKUP&&&&&&& interface eth0&&&&&&& virtual_router_id 51&&&&&&& mcast_src_ip 10.29.9.201&&&&&&& priority 100&&&&&&& authentication {&&&&&&&&&&&&&&&&&&&& auth_type PASS&&&&&&&&&&&&&&&&&&&& auth_pass&1111&&&&&&& }&&&&&&& track_script {&&&&&&&&&&&&&&& chk_http_port&&&&&&& }&&&&&&& virtual_ipaddress {&&&&&&&&&&&&&&&& 10.29.9.188&&&&&&& }}启动keepalived,检查虚拟IP是否邦定,在主keepalived[root@xenv0 ~]# /etc/init.d/keepalived start启动 keepalived:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& [确定][root@xenv0 ~]# ip a1: lo: &LOOPBACK,UP,LOWER_UP& mtu 16436 qdisc noqueue&&& link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00&&& inet 127.0.0.1/8 scope host lo2: eth0: &BROADCAST,MULTICAST,UP,LOWER_UP& mtu 1500 qdisc pfifo_fast qlen 1000&&& link/ether 00:16:36:68:a4:fc brd ff:ff:ff:ff:ff:ff&&& inet 10.29.9.200/24 brd 10.29.9.255 scope global eth0inet 10.29.9.188/32 scope global eth0在eth0上面我们已经看到虚拟IP 10.29.9.188已经邦定成功&4.安装tomcat 1)安装tomcat_1 tar zxvf apache-tomcat-7.0.23.tar.gz mv apache-tomcat-7.0.23 /usr/local/tomcat 2)安装tomcat_2,步骤同1)5.分别在tomcat服务器安装jdktar zxvf &jdk-7u2-linux-x64.tar.gzmv jdk1.7.0_02 /usr/local/jdk1.7.0_02cat &&/etc/profile &&EOFexport JAVA_HOME=/usr/local/jdk1.7.0_02export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/binEOF;source /etc/profile //使环境变量立即生效5.tomcat集群配置 tomcat1配置: 修改conf/server.xml配置文件&Engine name=&Catalina& defaultHost=&localhost& jvmRoute=&tomcat1&& &Cluster className=&org.apache.catalina.ha.tcp.SimpleTcpCluster& channelSendOptions=&8&& &Manager className=&org.apache.catalina.ha.session.DeltaManager& expireSessionsOnShutdown=&false& notifyListenersOnReplication=&true&/& &Channel className=&org.apache.catalina.tribes.group.GroupChannel&& &Membership className=&org.apache.catalina.tribes.membership.McastService& address=&224.0.0.4& port=&45564& frequency=&500& dropTime=&3000&/& &Receiver className=&org.apache.catalina.tribes.transport.nio.NioReceiver& address=&10.29.9.202&&//tomcat1 所在服务器的IP地址port=&4000& //端口号autoBind=&100& selectorTimeout=&5000& maxThreads=&6&/& &Sender className=&org.apache.catalina.tribes.transport.ReplicationTransmitter&& &Transport className=&org.apache.catalina.tribes.transport.nio.PooledParallelSender& /& &/Sender& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.TcpFailureDetector&/& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor&/& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor&/& &/Channel& &Valve className=&org.apache.catalina.ha.tcp.ReplicationValve& filter=&&/& &Valve className=&org.apache.catalina.ha.session.JvmRouteBinderValve&/& &Deployer className=&org.apache.catalina.ha.deploy.FarmWarDeployer& tempDir=&/tmp/war-temp/& deployDir=&/tmp/war-deploy/& watchDir=&/tmp/war-listen/& watchEnabled=&false&/& &ClusterListener className=&org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener&/& &ClusterListener className=&org.apache.catalina.ha.session.ClusterSessionListener&/& &/Cluster& 在&host&&&/host&添加下面这句:&Context path=&& docBase=&/opt/project & reloadable=&false& crossContext=&true& /&&tomcat2配置: 修改conf/server.xml配置文件&Engine name=&Catalina& defaultHost=&localhost& jvmRoute=&tomcat2&& &Cluster className=&org.apache.catalina.ha.tcp.SimpleTcpCluster& channelSendOptions=&8&& &Manager className=&org.apache.catalina.ha.session.DeltaManager& expireSessionsOnShutdown=&false& notifyListenersOnReplication=&true&/& &Channel className=&org.apache.catalina.tribes.group.GroupChannel&& &Membership className=&org.apache.catalina.tribes.membership.McastService& address=&224.0.0.4& port=&45564& frequency=&500& dropTime=&3000&/& &Receiver className=&org.apache.catalina.tribes.transport.nio.NioReceiver& address=&10.29.9.203& //tomcat2所在服务器IPport=&4001& //端口号不能和tomcat1重复autoBind=&100& selectorTimeout=&5000& maxThreads=&6&/& &Sender className=&org.apache.catalina.tribes.transport.ReplicationTransmitter&& &Transport className=&org.apache.catalina.tribes.transport.nio.PooledParallelSender& /& &/Sender& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.TcpFailureDetector&/& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor&/& &Interceptor className=&org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor&/& &/Channel& &Valve className=&org.apache.catalina.ha.tcp.ReplicationValve& filter=&&/& &Valve className=&org.apache.catalina.ha.session.JvmRouteBinderValve&/&&Deployer className=&org.apache.catalina.ha.deploy.FarmWarDeployer& tempDir=&/tmp/war-temp/& deployDir=&/tmp/war-deploy/& watchDir=&/tmp/war-listen/& watchEnabled=&false&/& &ClusterListener className=&org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener&/& &ClusterListener className=&org.apache.catalina.ha.session.ClusterSessionListener&/& &/Cluster&在&host&&&/host&添加下面这句:&Context path=&& docBase=&/opt/project & reloadable=&false& crossContext=&true& /&&6.session配置修改web应用里面WEB-INF目录下的web.xml文件,加入标签 &distributable/& 直接加在&/web-app&之前 开启网卡组播功能: route add -net 224.0.0.0 netmask 240.0.0.0 dev eth07.创建jsp测试页面 mkdir /opt/projectcd /opt/project vi index.jsp &html& &title& tomcat1 jsp &/title& &% String showMessage=&Hello,This is 10.29.9.202 server&; out.print(showMessage); %& &/html& ---------------------------- mkdir /opt/projectcd /opt/project vi index.jsp &html& &title& tomcat2 jsp &/title& &% String showMessage=& Hello,This is 10.29.9.203 server&; out.print(showMessage); %& &/html&&本文出自 “网事” 博客,请务必保留此出处http://gxjluck./814
无相关信息实战Nginx负载均衡高冗余高可用WEB架构
互联网行业交流群: 加群验证:FWQTG.NET
实战Nginx负载均衡高冗余高可用WEB架构
& &最近公司主力网站之一改版完成终于上线了,牵扯了我大半年的时间,现在终于有时间坐下来写点东西,总结沉淀一下自己的技术心得。此次,根据服务器的数量和质量,我采用负载均衡高冗余的架构,考虑单点故障,WEB也抛弃了apache,而使用的是nginx,数据库还是使用主、从架构。该架构目前承载80W的PV,没有大的压力。
这里简单谈一下web的选择疑问,是使用nginx还是apache,很多朋友在规划网站的时候都出现难以选择的问题,甚至有朋友在建设初用apache后期改成nginx。接下来我说一下我的规划选择,WEB究竟是使用Apache还是nginx,我认为两者各自之间优缺点非常明显,最终决定是要根据你的网站自身情况来确定,如网页的主要内容、网站类型(电商还是门户等等)。总之,在考虑过网站内容、类型等方面后还是无法选择,这时,大并发量我就选择nginx,而动态请求非常频繁的且并发量不大的网站我就使用apache。而我的这个网站是门户网站,内容基本都生成静态页,动态页面不多并且过不了多长时间就会生成html,所以我选择使用nginx做web。
&&& 首先声明,此架构适合场景:中小型网站场景,粗略预估100W PV左右。
主nginx:192.168.1.158
Web服务器一:192.168.1.163
Web服务器二:192.168.1.162
主数据库服务器:192.168.1.159
从数据库+备nginx:192.168.1.161
Nginx虚拟IP:192.168.1.160
操作系统:centos 6.4
系统的架构图,如下:
650) this.width=650;” style=”width:739height:491” title=”sfjsklj.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1T9FdDghbb9AAJSWX3ErvA471.jpg” width=+ height=+ alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
&& &一、YUM和编译安装软件方式的小分享
编译安装还是yum安装软件的争议,现在这个时代了还有人去争论。我认为这个事情没必要争论,也不可能争论出结果,完全看个人的工作习惯,我想说YUM真的很节省时间,YUM安装软件简单、快捷,还有点好处可以培养新人的linux兴趣。编译安装可以很好订制化,将不需要的功能关闭掉,减少安全隐患等。YUM安装只要关闭不需要的功能结果和编译安装一样的,而编译安装过程中你很详细的掌握了软件的安装路径等,真是各有优缺点,没必要这上面去浪费心思。而我是按照所需时间来选择,基本上我是个没时间的人,所以我大多数都使用yum&方式安装软件。
二、主、从mysql安装及同步
&&& 挺简单的配置,如果感兴趣的朋友可以参看我上篇文章《mysql linux下高可用架构的简析及主、从复制的实战(一)》
&& 三、PHP安装
&& 在两台WEB上分别安装PHP,安装过程非常简单。
1、yum install php*
service php-fpm start 启动php-fpm
2、调整PHP时区
要注意先调整一下php的时区和时间,如果不调整会出现PHP与系统时间不对应的错误。在php.ini找到;Date.timezone =&& 并将;去掉,改成下图
&650) this.width=650;” title=”shiqu.png” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZvhHT_O4_AACM8zgRLOY656.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
3、安装zend加速
一定要安装,php加速确实能提高PHP的运行速度,不装ZEND也至少装一种加速软件。
之前安装过ZEND包,所以我直接拿来用,上传后解压在规定目录内。
打开php.ini配置文件,在最后添加zend的配置代码
650) this.width=650;” title=”zend.png” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKioL1TZv1qS7-8MAAD0nFL_XTw403.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
重启php生效,service php-fpm restart
四、web服务器的安装
因为我是个时间紧张的人,所以我采用Yum安装。Centos 6.4本身的yum源是没有nginx安装包的,所以我先换源换成atomic的源,再yum安装。
wget /installers/atomic,直接下载
Sh ./atomic
650) this.width=650;” title=”sh.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZw03infc4AAUXBGRMBb4930.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
&&&&&& 默认安装就可以,装完后会看到/etc/yum.repos.d/ 会有atomic.repo
为让更换源立即生效,输入yum check-update 让YUM自己check。
2、安装nginx
Yum install nginx
安装后nginx版本是1.6.2-23,正是我想装的 1.6。
650) this.width=650;” title=”nginx版本.png” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZw72QdAIaAACBZReNvek137.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
修改配置mv /etc/nginx/nginx.conf /etc/nginx/nginx.bak,建立虚拟主机目录mkdir Cp /etc/nginx/vhosts/
将这段调整好的代码写入新的nginx.conf
##############################################################
# This is the main Nginx configuration file.&
# More information about the configuration options is available on
#&& * the English wiki – http://wiki.nginx.org/Main
#&& * the Russian documentation – http://sysoev.ru/nginx/
#######################################################################
#———————————————————————-
# Main Module – directives that cover basic functionality
#&& http://wiki.nginx.org/NginxHttpMainModule
#———————————————————————-
user&& root&
worker_processes 8;
error_log& /var/log/nginx/error.
pid&&&&&&& /var/run/nginx.
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
& worker_connections 65535;
& include&&&&&& /etc/nginx/mime.
& default_type& application/octet-
& ssi_silent_
& ssi_types test/
& #charset& gb2312;
& server_names_hash_bucket_size 128;
& client_header_buffer_size 32k;
& large_client_header_buffers 4 32k;
& client_max_body_size 8m;
& tcp_nopush&&&&
& keepalive_timeout 60;
& fastcgi_connect_timeout 300;
& fastcgi_send_timeout 300;
& fastcgi_read_timeout 300;
& fastcgi_buffer_size 64k;
& fastcgi_buffers 4 64k;
& fastcgi_busy_buffers_size 128k;
& fastcgi_temp_file_write_size 128k;
& fastcgi_intercept_
& gzip_min_length& 1k;
& gzip_buffers&&&& 4 16k;
& gzip_http_version 1.0;
& gzip_comp_level 2;
& gzip_types&&&&&& text/plain application/x-javascript text/css application/
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& /var/log/nginx/access.log&
&&& include /etc/nginx/vhosts/*.
3、在/etc/nginx/vhosts/ 创建网站站点
Vi www.conf
&&& listen&&&&&& 80;
&&& server_name& ;
&&& index index.html index.
&&& root& /www/web_
&& &location ~ .*/.(php|php5)?$
&&& {&&&&&
&&&&& #fastcgi_pass& unix:/tmp/php-cgi.
&&&&& fastcgi_pass& 127.0.0.1:9000;
&&&&& fastcgi_index index.
&&&&& include fastcgi.
650) this.width=650;” title=”ngxinconf.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZxP7gKSJ_AAFAFGNQz1g600.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
两台WEB服务器做一样的配置。
五、负载均衡的实现
&& &nginx做负载均衡还是很稳定,甚至不需要脚本来监控nginx进程存活情况,我维护的几个站基本一年有不了一、两次,所以没有编写也没有采用别人写的监控进程脚本,但是提醒各位朋友,因网站而异,没有什么该有什么不该有,适合自己的就是最好、最稳定的。
1、负载均衡nginx的安装及配置
两台nginx分别安装nginx,过程简单就不再详细说明,贴出配置文件供各位朋友参考。
worker_processes 8;
pid&&&&&&& /var/run/nginx.
worker_rlimit_nofile 65535;
worker_connections 65535;
include&&&&&& mime.
default_type application/octet-
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
tcp_nopush&&& &
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip_min_length 1k;
gzip_buffers&&&& 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types&&&& text/plain application/x-javascript text/css application/
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 /var/log/nginx/access.
upstream backend {
&& server 192.168.1.162;
&& server 192.168.1.163;
listen 80;
location / {&
root /var/
index index.htm index.html index.php index.
proxy_set_header Host $
proxy_set_header X-Real-IP $remote_
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_
proxy_pass http://
2、yum 安装keepalived
Yum install keepalived
650) this.width=650;” title=”keepalived.png” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZxT-ht0gUAACxSMpoo40999.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
3、主keepalived的配置
Vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
&& router_id LVS_DEVEL
vrrp_instance VI_1 {
&&& state MASTER
&&& interface eth1
&&& virtual_router_id 51
&&& priority 99&
&&& advert_int 1
&&& authentication {
&&&&&&& auth_type PASS
&&&&&&& auth_&&&&&
&&& virtual_ipaddress {
&&&&&&&& 192.168.1.160
650) this.width=650;” title=”keeconf.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZxYnhrtldAAFVdNbH9dU933.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
Service keepalived start
后ip add 的结果如下,出现global ip& 就成功了。
650) this.width=650;” title=”ipadd.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKioL1TZxrPA4hgHAAGSZz9mV-E134.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
4、副keepalived的配置
! Configuration File for keepalived
global_defs {
&& router_id LVS_DEVEL
vrrp_instance VI_1 {
&&& state BACKUP
&&& interface eth1
&&& virtual_router_id 51
&&& priority 99&
&&& advert_int 1
&&& authentication {
&&&&&&& auth_type PASS
&&&&&&& auth_&&&&&
&&& virtual_ipaddress {
&&&&&&&& 192.168.1.160
650) this.width=650;” title=”fukee.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKiom1TZxfLzYopQAAE0nL3Dbe4302.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
测试过程如下:
1、分别重启主nginx和备nginx服务器,观察网站访问情况,也通过ping 虚拟IP地址192.168.1.160来观察,看能否正常访问网站。
通过ping是比较直观的,只丢失1个包,网站是能正常打开。
2、分别重启web1和web2服务器,观察网站访问情况。
检查个站点打开情况,打开完整的。
结果:如果两步测试结果都不影响网站的正常访问,说明负载均衡架构是成功的,恭喜你,是不是有种建设成功后的小喜悦。
1、两台web服务器的nginx都设置了worker_connections 65535; 必须优化centos的内核,否则是起不到作用的。
vi /etc/sysctl.conf
在sysctl.conf代码的最后面加入
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =& 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max =
net.core.wmem_max =
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 000000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.ip_local_port_range =
650) this.width=650;” title=”neihe.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKioL1TZx1eS2_teAAJD96lUsL8289.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />
重启服务器或/sbin/sysctl Cp
2、nginx日志切分,要是你使用我的nginx日志部分的配置,就不用再去更改,每晚凌晨会自动切分,不过log日志路径要放在/var/log/nginx目录下。
650) this.width=650;” title=&#.PNG” src=”http://www.fwqtg.net/wp-content/uploads/2015/07/wKioL1TZx4PywNW3AABrH0I4zUY444.jpg” alt=”实战Nginx负载均衡高冗余高可用WEB架构” />&
本文出自 “踏酒浅酌” 博客,请务必保留此出处http://hostslinux./3387

我要回帖

更多关于 keepalived原理 的文章

 

随机推荐