您的位置:首页 > 运维架构 > Tomcat

nginx+tomcat做负载时,当一台服务器宕机如何解决?

2016-10-21 22:16 507 查看
在做负载均衡时遇到一个问题,已经实现负载均衡,当关闭一个正在运行的项目时,发现不能自动访问到另一个项目,这令我百思不得其解,后来在网上搜了一下这个问题,发现有人也遇到这个问题,并找出了解决的方案,如下代码所示:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
worker_connections  1024;
}

http {
include       mime.types;
default_type  application/octet-stream;

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
listen       80;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;
upstream localhost {
#这里是在同一台服务器上面进行测试,因此只改变端口号即可
#ip_hash;
server localhost:8080 weight=5; #这个可以配置权重,权重越大,访问该服务器的机率就越大
server localhost:8081 weight=2;
}
location / {
root   html;
index  index.html index.htm; http://localhost; //可通过域名访问

proxy_connect_timeout   1; #单位为秒
proxy_send_timeout      1;
proxy_read_timeout      1;
}

#error_page  404              /404.html;

......
由于下面是全是注释,也没有用到,因此此处省略N行,嘻嘻。。。

}


proxy_connect_timeout

语法:proxy_connect_timeout time ;

该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。说明 :该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

proxy_send_timeout

语法 proxy_send_timeout time ;

默认值 60s

说明: 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

proxy_read_timeout

语法 proxy_read_timeout time ;

默认值 60s

说明: 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

在http模块内配置了这三个字段,再reload 一下,只启动一个项目,就会发现,就算宕机一台,我们的项目也可以接着使用,如果不放心,可以多试几次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  负载均衡
相关文章推荐