1 Nginx + 12 Tomcat +2DB 实现2个程序负载均衡

根据真实生产环境 总结。

硬件:共计5台服务器  1台负载windows、2台业务windows、2台数据库linux
业务:运行两个程序,两个数据库
具体:63服务器安装 Nginx 做负载 ;61和62服务器各安装6个Tomcat  跑业务1和业务2 ;59和60各跑一个数据库;

备注:Nginx最好安装在Linux服务器上,因为软件开发公司原因,这里选择安装到windows服务器上


 63服务器上nginx开两个端口 8001、8011 ,每个端口都是一个集群接口
8001对应6个tomcat(分布在61、62两台机器上)
8011对应6个tomcat(分布在61、62两台机器上)
红色跑的服务1、蓝色跑的服务2;两数据库互相独立,没有负载设置。
服务1和服务2  各由6个tomcat支撑。
 1 Nginx + 12 Tomcat +2DB  实现2个程序负载均衡 - 朝鲜程序员 - 朝鲜程序员的博客

 

OK,现在我们可以开始配置Nginx来实现负载均衡了,其实非常的简单,只需要配置好Nginx的配置文件即可


修改nginx.conf

#nginx命令
#nginx -t text          configuration file
#nginx -s stop        quick exit
#nginx -s quit        graceful quit
#nginx -s reloadchanging configuration, starting a new worker, quitting an old worker gracefully
#nginx -s reopenreopening log files

#运行用户
#user  nobody;

#工作进程,根据硬件调整,大于等于cpu核数
worker_processes  16;
#映射目录
include       D:/nginx/conflist/*.conf;      集群具体配置放在这里

#等待事件
events {
    #每个进程最大连接数(最大连接=连接数x进程数)
    worker_connections  1024;
}





在 D:/nginx/conflist/文件下 有两个文件    nginx_group1.conf和nginx_group2.conf
分别是两个集群的配置信息,由于两个文件配置信息大部分相同,下面只讲解nginx_group1.conf


#错误日志保存位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程号保存文件
#pid        logs/nginx.pid;

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  60;

    #打开gzip压缩
    #gzip  on;
    
    #设定负载均衡的服务器列表
    #1-轮询(默认)
    #     每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    #2-weight
    #     指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    #     参数weigth参数表示权值,权值越高被分配到的几率越大
    #3-ip_hash
    #     每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以
    #     解决session的问题。
    #4-fair
    #     这种模式会根据后端服务的响应时间来分配,响应时间短的后端优先分配
    upstream mysvr {
            #ip_hash; 
            server 10.10.12.61:8101;
            server 10.10.12.61:8102;
            server 10.10.12.61:8103;   
            server 10.10.12.62:8101;
            server 10.10.12.62:8102;
            server 10.10.12.62:8103;    
    }

    #第一个虚拟主机
    server {
        #监听IP端口
        listen       8001;
        #主机名
        server_name  localhost;

        #设置字符集
        #charset koi8-r;

        #本虚拟server的访问日志 相当于局部变量
        #access_log  logs/host.access.log  main;

        location / {
            root   html;                      # 指向nginx 安装目录下的html文件夹,看具体配置  
            index  index.html index.htm;
            #proxy_pass  http://mysvr ;       #请求转向mysvr 定义的服务器列表
        }

        #中间层转向mysvr进行负载均衡
        location /JWebService {
            #root   html;
            #index  index.html index.htm;
            proxy_pass  http://mysvr ;       #请求转向mysvr 定义的服务器列表
            
            #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            #client_max_body_size 10m;                      #允许客户端请求的最大单文件字节数
            #client_body_buffer_size 128k;                  #缓冲区代理缓冲用户端请求的最大字节数,
            #proxy_connect_timeout 90;                      #nginx跟后端服务器连接超时时间(代理连接超时)
            #proxy_send_timeout 90;                         #后端服务器数据回传时间(代理发送超时)
            #proxy_read_timeout 90;                         #连接成功后,后端服务器响应时间(代理接收超时)
            #proxy_buffer_size 4k;                          #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            #proxy_buffers 4 32k;                           #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
            #proxy_busy_buffers_size 64k;                   #高负荷下缓冲大小(proxy_buffers*2)
            #proxy_temp_file_write_size 64k;                #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }

        #文件访问直接指向磁盘文件
        location /FileService {
           ##root alias 
           #location ~ ^/awstats/ {
           #root  /home/awstats/;
           #访问:http://test.com/awstats/ 实际访问的是/home/awstats/awstats/

           #location ~ ^/awstats/ {                        #使用alias时目录名后面一定要加“/”
           #alias  /home/awstats/;
           #访问:http://test.com/awstats/ 实际访问的是/home/awstats/

           root D:efuture;    ##会指向D:efutureFileService 
           autoindex on;       ##会自动显示资源目录  
           index noindex.htm;   
        }

        #访问/tomcat1 的将重新定向到http://127.0.0.1:8381/
        location /tomcat1/ { 
           proxy_pass       http://127.0.0.1:8381/;
           index  index.html index.htm;
        } 

        #访问/tomcat2 的将重新定向到http://127.0.0.1:8382/
        location /tomcat2/ { 
           proxy_pass       http://127.0.0.1:8382/;
           index  index.html index.htm;
        } 

        #设定查看Nginx状态的地址
        location /NginxStatus {
            stub_status on;
            access_log on;
            auth_basic "NginxStatus";
            auth_basic_user_file conf/htpasswd;
        }

        #目录重定向,访问/phpadmin/ 的将重新定向到http://_/phpadmin
        #location /phpadmin/ { 
        #   alias   /opt/www/phpadmin/; 
        #   index   index.php; 
        #} 

        #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;
    #    }
    #}

}



第二个集群   nginx_group2.conf    设置,主要配置一下内容
 upstream mysvr {
            #ip_hash; 
            server 10.10.12.61:8201;
            server 10.10.12.61:8202;
            server 10.10.12.61:8203;   
            server 10.10.12.62:8201;
            server 10.10.12.62:8202;
            server 10.10.12.62:8203;    
    }

    #第一个虚拟主机
    server {
        #监听IP端口
        listen       8011;
        #主机名
        server_name  localhost;














首先在61和62上安装  Tomcat  
分别解压出6个Tomcat,分别命名为apache-tomcat-1和apache-tomcat-2  apache-tomcat-3……

然后修改这6个Tomcat的启动端口,分别设为8101、8102、8103、8201、8202、8203  
打开Tomcat的conf目录下的server.xml,修改以下配置:
<Connector port="8101" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8102" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
 
<Connector port="8103" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
……

分别启动tomcat 看是否正常

我们可以修改每个tomcat的主页,添加内容,在后面测试时便于区分
原文地址:https://www.cnblogs.com/centos2017/p/7896684.html