首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

nginx 负载均衡时,一台tomcat宕机时的问题 自动切换

往期精选

如果Nginx没有仅仅只能代理一台服务器的话,那它也不可能像今天这么火,Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用。具体配置过程如下:

1. 在http节点下,添加upstream节点。

upstream linuxidc {

server 10.0.6.108:7080;

server 10.0.0.85:8980;

}

2.将server节点下的location节点中的proxy_pass配置为:http:// + upstream名称,即“

http://linuxidc”.

location / {

roothtml;

indexindex.html index.htm;

proxy_pass http://linuxidc;

}3.现在负载均衡初步完成了。upstream按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能 自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。适用于图片服务器集群和纯静态页面服务器集群。

除此之外,upstream还有其它的分配策略,分别如下:

weight(权重)

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如下所示,10.0.0.88的访问比率要比10.0.0.77的访问比率高一倍。

upstream linuxidc{

server 10.0.0.77 weight=5;

server 10.0.0.88 weight=10;

}

ip_hash(访问ip)

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

upstream favresin{

ip_hash;

server 10.0.0.10:8080;

server 10.0.0.11:8080;

}

fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。与weight分配策略类似。

upstream favresin{

server 10.0.0.10:8080;

server 10.0.0.11:8080;

fair;

}

url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

注意:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

upstream resinserver{

server 10.0.0.10:7777;

server 10.0.0.11:8888;

hash $request_uri;

hash_method crc32;

}

upstream还可以为每个设备设置状态值,这些状态值的含义分别如下:

down 表示单前的server暂时不参与负载.

weight 默认为1.weight越大,负载的权重就越大。

max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.

fail_timeout : max_fails次失败后,暂停的时间。

backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

upstream bakend{ #定义负载均衡设备的Ip及设备状态

ip_hash;

server 10.0.0.11:9090 down;

server 10.0.0.11:8080 weight=2;

server 10.0.0.11:6060;

server 10.0.0.11:7070 backup;

}

Nginx的配置与部署研究,Upstream负载均衡模块http://www.linuxidc.com/Linux/2013-04/82526p2.htm

CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

用了nginx负载均衡后,在两台tomcat正常运行的情况下,访问http://localhost 速度非常迅速,通过测试程序也可以看出是得到的负载均衡的效果,但是我们试验性的把其中一台tomcat(server localhost:8080)关闭后,再查看http://localhost,发现反应呈现了一半反映时间快,一半反映时间非常非常慢的情况,但是最后都能得到正确结果.

解决办法:

问题解决,主要是proxy_connect_timeout这个参数, 这个参数是连接的超时时间。 我设置成1,表示是1秒后超时会连接到另外一台服务器。

#usernobody;

worker_processes1;

#error_loglogs/error.log;

#error_loglogs/error.lognotice;

#error_loglogs/error.loginfo;

#pidlogs/nginx.pid;

events {

worker_connections1024;

}

http {

includemime.types;

default_typeapplication/octet-stream;

upstream localhost {

#ip_hash;

server 127.0.0.1:8081;

server 127.0.0.1:8080;

}

#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '

#'$status $body_bytes_sent "$http_referer" '

#'"$http_user_agent" "$http_x_forwarded_for"';

#access_loglogs/access.logmain;

sendfileon;

#tcp_nopushon;

#keepalive_timeout0;

keepalive_timeout65;

#gzipon;

server {

listen80;

server_namelocalhost;

listen 80;

server_name localhost;

location /{

proxy_pass http://localhost;

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_connect_timeout1;

proxy_read_timeout1;

proxy_send_timeout1;

}

#charset koi8-r;

#access_loglogs/host.access.logmain;

#error_page404/404.html;

# redirect server error pages to the static page /50x.html

#

error_page500 502 503 504/50x.html;

location = /50x.html {

roothtml;

}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#

#location ~ \.php$ {

#proxy_passhttp://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ \.php$ {

#roothtml;

#fastcgi_pass127.0.0.1:9000;

#fastcgi_indexindex.php;

#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;

#includefastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#denyall;

#}

}

# another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#listen8000;

#listensomename:8080;

#server_namesomenamealiasanother.alias;

#location / {

#roothtml;

#indexindex.html index.htm;

#}

#}

# HTTPS server

#

#server {

#listen443;

#server_namelocalhost;

#sslon;

#ssl_certificatecert.pem;

#ssl_certificate_keycert.key;

#ssl_session_timeout5m;

#ssl_protocolsSSLv2 SSLv3 TLSv1;

#ssl_ciphersALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

#ssl_prefer_server_cipherson;

#location / {

#roothtml;

#indexindex.html index.htm;

#}

#}

}

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20171219B0BE5W00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券