nginx+tomcat实现负载均衡

目标:在windows环境下搭建nginx+tomcat实现负载均衡

1、什么是nginx?

nginx是一款高性能的http 服务器、反向代理服务器及电子邮件(IMAP/POP3)代理服务器。nginx具有良好的稳定性、功能集、示例配置文件和低系统资源的消耗。

2、nginx应用场景

2-1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做静态网页服务器。 
2-2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。 
2-3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

3、windows环境下安装nginx

首先下载nginx服务器,地址:http://nginx.org/en/download.html

这里写图片描述

我们在windows下载zip包解压,然后点击里面的nginx.exe,画面闪一下就将nginx启动了。在浏览器中输入http:localhost可以测试nginx是否启动成功了(默认使用80端口,80端口号在浏览器中默认不显示出来),成功画面如下:

这里写图片描述

关闭nginx:进入到nginx安装目录,nginx -s stop 或启动任务管理器结束nginx进程

这里写图片描述

关闭之后将不能访问http:localhost了。


4、使用nginx实现反向代理

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。 
Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。

案例:使用nginx实现同一端口根据不同域名转发到不同地址 
输入haha.8080 跳转到 127.0.0.1:8080 
输入haha.8081 跳转到 127.0.0.1:8081 
这两个输入地址域名不同,但是端口号都是默认的80;

4-1、建两个端口号分别为8080、8081的tomcat服务器 
修改一个tomcat的端口号有三个地方需要修改:关闭端口号、连接端口号、重定向的端口号

<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

tomcat访问的默认首页为webapps\ROOT下的index.html文件,我们可以修改index.html内容来区分访问的是8080还是8081服务器。 
比如在index.html中只写一个<h1>这是8080服务器</h1> 
最后分别访问8080、8081服务器测试两个tomcat是否都启动成功。

4-2、配置本地域名 
在本地host文件配置(C:\Windows\System32\drivers\etc)本地域名和对应的服务器请求地址,即用域名代替ip地址。

127.0.0.1 haha.8080
127.0.0.1 haha.8081

这时候可以在浏览器输入http://haha.8080:8080http://haha.8081:8081分别访问刚才启动的两台服务器。 
现在我想http://haha.8080http://haha.8081这两个地址不输入端口号就能访问两台服务器该怎么办?

4-3、在nginx中配置虚拟主机进行反向代理跳转,跳转的端口号默认都是80。 
在nginx安装目录conf文件中找到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 tomcat{
        server 127.0.0.1:8091 weight=3;
        server 127.0.0.1:8092 weight=4;
        server 127.0.0.1:8093 weight=4;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;

            proxy_pass http://tomcat;
            proxy_connect_timeout 2;
            proxy_read_timeout 3;
            proxy_send_timeout 5;
        }
        #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;
    #    }
    #}

}

通过以上其实我们也可以将server_name改成我们自己的ip127.0.0.1,但是当有两个server_name是一样的时候,它只会选择监听先配置的那个server。 
那么我们现在这个配置我们除去4-2在本地配置域名那一步,直接在浏览器中输入haha.8080 nginx 能不能监听到呢?不能!因为标准的http请求是ip+端口,没有配置域名那一步就不能解析出ip地址。 
至此,nginx反向代理完成,浏览器输入haha.8080和haha.8081请求端口号都是默认的80,实现了同一端口号根据不同域名找到不同地址。


5、nginx实现负载均衡

什么是负载均衡? 
负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。 
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。 
如下图: 
这里写图片描述
下面开始:Windows环境下搭建nginx+tomcat负载均衡 
nginx负载均衡服务器:127.0.0.1 
tomcat1服务器:127.0.0.1:8080 
tomcat2服务器:127.0.0.1:8081 
真实环境中nginx、tomcat服务器是分开的。 
5-1、在host文件中进行域名配置

127.0.0.1  fzjh.com
  • 1

5-2、nginx进行负载均衡及权重配置

#负载均衡配置
    upstream myserver {

        server 127.0.0.1:8080 weight=2;

        server 127.0.0.1:8081 weight=1;
    }

    server {

            listen       80;

            server_name  fzjh.com;

            location / {

                 #反向代理的地址

                 proxy_pass http://myserver;     

            }

    }

至此负载均衡配置完成。重启nginx服务器,然后浏览器访问fzjh.com就会发现多次请求会根据权重分别到不同的服务器上。 
关于nginx配置简单说明:

节点说明:
在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越大,负载的权重就越大。当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误 
fail_timeout:max_fails 次失败后,暂停的时间。 
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值