简单应用nignx + tomcat 实现服务器的负载均衡php
当服务器有高并发访问亦或是须要更新项目重启服务器的时候,为了防止服务器没法访问,须要配置服务器集群,当有服务器宕机的时候,nginx会将请求分发到能够访问,压力较小的服务器上。html
下面是我本地作的一个简单的测试: 须要: tomcat*2 + nginxnginx
分别配置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次2tomcat
这里只是简单的一个demo,固然在实际管理工做中还有更多的配置服务器
扩展:更多属性配置介绍session
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正常运行的状况下,访问http://localhost 速度很是迅速,经过测试程序也能够看出是获得的负载均衡的效果,可是咱们试验性的把其中一台tomcat(server localhost:8080)关闭后,再查看http://localhost,发现反应呈现了一半反映时间快,一半反映时间很是很是慢的状况,可是最后都能获得正确结果.
解决办法: 问题解决,主要是proxy_connect_timeout 这个参数, 这个参数是链接的超时时间。 我设置成1,表示是1秒后超时会链接到另一台服务器。这里能够配合fail_timeout 一块儿使用,即,若是链接超过配置的时间后,就让访问的这个服务器 fail_timeout时间内再也不转发给它请求(当成它宕机)