博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx+tomcat 负载均衡配置
阅读量:7080 次
发布时间:2019-06-28

本文共 4542 字,大约阅读时间需要 15 分钟。

hot3.png

简单应用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时间内不再转发给它请求(当成它宕机)

更多参数可参考下方

转载于:https://my.oschina.net/u/1417838/blog/1537563

你可能感兴趣的文章
lsof -i -n -P
查看>>
RocketMQ高并发读写
查看>>
Kali linux上运行quasibot时主页不显示内容的问题
查看>>
CentOS 5.5下搭建Mysql+DRBD+Heartbeat
查看>>
端口镜像
查看>>
apache 虚拟主机配置
查看>>
升级nuxt2.0后遇到的问题
查看>>
c++缺陷与不足
查看>>
异常java.lang.NullPointerException
查看>>
我必须得告诉大家的MySQL优化原理
查看>>
几种 hive join 类型简介
查看>>
用 Asterisk 搭建自己的免费 VoIP 服务器
查看>>
安装配置ASMlib驱动
查看>>
如何将24位RGB颜色转换16位RGB颜色
查看>>
案例:部署kvm虚拟化平台
查看>>
DHCP服务器的搭建与配置
查看>>
Hibernate session FlushMode浅要分析
查看>>
实战CentOS系统部署Hadoop集群服务
查看>>
前端之viewport
查看>>
更改pip源
查看>>