Nginx+Tomcat实现负载均衡

来源:互联网 发布:python怎么多线程并发 编辑:程序博客网 时间:2024/05/24 03:53

先安装配置2个tomcat,修改三个端口号:SHUTDOWN Connector (http) Connector(AJP),我的设置是第一个tomcat 端口是18080,第二个tomcat28080;

修改tomcat webapp下的ROOT下的index.jsp内容区分2个tomcat;

nginx修改配置:

#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 testnginx{        server 127.0.0.1:18080 weight=1;        server 127.0.0.1:28080 weight=2;    }    server {        listen       80;        server_name  www.testnginx.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            proxy_pass   http://testnginx;            proxy_redirect default;        }

worker_processes   工作进程的个数,一般与计算机的cpu核数一致;

sendfile    开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。

upstream   服务器集群 名字随便,server的weight数字越大被访问的几率越高;  

server    当前的nginx配置;

server_name  访问nginx的域名;

location proxy_pass 要和proxy_pass名字呼应;


启动tomcat nginx

访问:www.testnginx.com 发现有时候访问的tomcat1有时候访问tomcat2,28080端口的tomcat是18080的访问几率的2倍;