简单应用nignx + tomcat 实现服务器的负载均衡
当服务器有高并发访问亦或是需要更新项目重启服务器的时候,为了防止服务器无法访问,需要配置服务器集群,当有服务器宕机的时候,nginx会将请求分发到可以访问,压力较小的服务器上。
下面是我本地做的一个简单的测试: 需要: tomcat*2 + nginx
分别配置2个tomcat的端口,我配置的是8080 和 18080; 并且配置tomcat的首页(index.jsp) 这里简单的配置显示1和2 启动2个tomcat
当访问localhost:8080 和 localhost:18080 的时候 可以访问成功
然后需要配置nginx 修改nginx/cong/nginx.conf
#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; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream www.lbsky.com { server 127.0.0.1:8080 weight=3; #weight 配置权重 server 127.0.0.1:18080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} location / { # 将localhost的访问 指向 www.lbsky.com proxy_pass http://www.lbsky.com; proxy_redirect default; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
然后访问localhost,发现 分别展现1,2 并且每得到3次1 然后得到1次2
这里只是简单的一个demo,当然在实际管理工作中还有更多的配置
扩展:更多属性配置介绍
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负载均衡后,在两台tomcat正常运行的情况下,访问 速度非常迅速,通过测试程序也可以看出是得到的负载均衡的效果,但是我们试验性的把其中一台tomcat(server localhost:8080)关闭后,再查看.
解决办法: 问题解决,主要是proxy_connect_timeout 这个参数, 这个参数是连接的超时时间。 我设置成1,表示是1秒后超时会连接到另外一台服务器。这里可以配合fail_timeout 一起使用,即,如果连接超过配置的时间后,就让访问的这个服务器 fail_timeout时间内不再转发给它请求(当成它宕机)
更多参数可参考下方