Nginx + Tomcat 搭建负载均衡

负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

1. 需要的工具

nginx-1.8.1.zip   点击下载
tomcat 8   点击下载

2. 实现的步骤

(1)解压 nginx 至相应的目录(本人目录: F:\jd\tomcat_nginx\nginx-1.8.1);
(2)打开文件夹 nginx-1.8.1 找到 nginx.exe 运行文件双击,如果界面一闪而过,输入地址: http://localhost 则会出现以下页面,nginx 的默认端口为 80;
在这里插入图片描述
(3) 解压 tomcat 至相应的目录(本人目录:F:\jd\tomcat_nginx\apache-tomcat-8.0.47-18080 和 F:\jd\tomcat_nginx\apache-tomcat-8.0.47-28080);

(4)修改 tomcat 端口为 18080 的 server.xml 文件(目录: F:\jd\tomcat_nginx\apache-tomcat-8.0.47-18080\conf)为以下内容,为避免启动程序出现错误,共修改了三处位置:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
(5)为了区别两个 tomcat 的差别,删除所有 apache-tomcat-8.0.47-18080\webapps\ROOT 目录下的所有文件,并且新建一个 index.jsp ,添加内容为 <h1>Tomcat 18080</h1>,启动 tomcat 服务输入 http://localhost:18080,如果成功则出现以下页面:
在这里插入图片描述
(6)修改 tomcat 端口为 28080 的 server.xml 文件,修改步骤重复第(4)(5)步;
(7)配置 nginx 来实现负载均衡,打开目录 F:\jd\tomcat_nginx\nginx-1.8.1\conf 找到 nginx.conf 文件并进行如下修改:
   在这里插入图片描述
  以上为主要的修改位置,下面是该文件的全部文件信息,添加了注解:

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

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

    #keepalive_timeout  0;
    keepalive_timeout  65;    # 长连接超时时间,单位是秒 

    #gzip  on;    # 启用Gizp压缩
    
    #服务器的集群  
    upstream yjq {  # 服务器集群名字   
        server    127.0.0.1:18080  weight=1;    # 服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
        server    127.0.0.1:8080  weight=2;  
    }   

    # 当前的Nginx的配置
    server {
        listen       88;    # 监听88端口,可以改成其他端口  
        server_name  localhost;    # 当前服务的域名 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm;
            proxy_pass http://yjq;  
            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;
    #    }
    #}

}

补充说明
    在http节点里添加:

        # 定义负载均衡设备的 Ip及设备状态 
      upstream myServer { 
        server 127.0.0.1:9090 down; 
        server 127.0.0.1:8080 weight=2; 
        server 127.0.0.1:6060; 
        server 127.0.0.1:7070 backup; 
      }

在需要使用负载的Server节点下添加
    proxy_pass http://myServer;
    upstream 每个设备的状态;
    down 表示当前的 server 暂时不参与负载 ;
    weight 默认为 1,weight 越大,负载的权重就越大;
    max_fails 允许请求失败的次数默认为 1,当超过最大次数时,返回 proxy_next_upstream 模块定义的错误 ;
    fail_timeout:max_fails 次失败后,暂停的时间;
    backup 其它所有的非 backup 机器 down 或者忙的时候,请求 backup 机器,所以这台机器压力会最轻;
  (8)最后输入网址 http://localhost 检测(三个服务都必须打开,tomcat18080、tomcat28080、nginx.exe)
  第一次打开的页面:
   在这里插入图片描述
  第二次打开的页面:
   在这里插入图片描述
  第三次打开的页面:
  
在这里插入图片描述
  第四次打开的页面:
   在这里插入图片描述
  所以,现在基本上完成了Nginx + Tomcat 搭建负载均衡;

附录

如果系统占用了 80 端口,导致 nginx 不能启动,可以通过 netstat -aon | findstr :80 命令查看80端口被谁占用,如果是系统占用,通过以下步骤解决:  
    1、打开注册表:win + R 输入 regedit;
    2、找到: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP;
    3、找到一个 REG_DWORD 类型的项 Start,将其改为 0;
    4、重启系统,System 进程不会占用 80 端口;

Nginx 常用命令(切换到 nginx 安装目录来执行):

start nginx.exe: 启动 nginx;

nginx.exe -s stop: 停止 nginx;

nginx.exe -s reload: 配置文件修改,重新加载配置文件;

nginx -t: 查看nginx是否启动成功;

nginx -v: 查看nginx版本;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值