您的位置:首页 > 运维架构 > Tomcat

Linux+Nginx+Tomcat实现Web服务器的负载均衡集群

2017-11-01 15:57 921 查看

一、环境准备

服务器名称系统版本预装软件IP地址
Nginx服务器CentOS 7 最小安装Nginx192.168.159.128
Web服务器ACentOS 7 最小安装tomcat+jdk192.168.159.128
Web服务器BCentOS 7 最小安装tomcat+jdk192.168.159.128
服务器采用CentOS 7 最小安装模式,采用一台服务器模拟生产环境中三台服务器,一台Nginx服务器,两台Tomcat服务器,实现一个简化的反向代理和负载均衡服务。 

二、原理图



三、前置条件

JDK安装见:http://blog.csdn.net/jay_1989/article/details/51443370

Tomcat安装见:http://blog.csdn.net/jay_1989/article/details/51453213

Tomcat修改默认端口号见:http://blog.csdn.net/jay_1989/article/details/52870760

Nginx安装见:http://blog.csdn.net/jay_1989/article/details/78410858

四、部署两台Tomcat

解压两个Tomcat,分别命名为apache-tomcat-7.0.29-1和apache-tomcat-7.0.29-2:



然后修改这两个Tomcat的启动端口,分别为18080和28080,下面以修改第一台Tomcat为例,打开Tomcat的conf目录下的server.xml:



共需修改3处端口:



当然第二台Tomcat也一样,如下图:



修改上面两个Tomcat的默认页面(为了区分下面到底访问的是那一台Tomcat,随便改一下即可):



然后启动两个Tomcat,并访问,看是否正常:





五、修改Nginx核心配置文件nginx.conf

下面配置文件中的几个关键点:

(1)进程数与每个进程的最大连接数

#工作进程个数,一般跟服务器cpu核数相等,或者核数的两倍
worker_processes 2;

#单个进程最大连接数
events{
worker_connections 1024;
}
① nginx进程数,建议设置为和服务器cup核数相等,或者是核数的两倍

② 单个进程最大连接数,该服务器的最大连接数=连接数*进程数; 

服务器支持最大并发数=(连接数*进程数) /2 ,因为反向代理是双向的。

(2)Nginx的基本配置

#nginx基本配置
server{
listen 8088;    #端口号
server_name 192.168.159.128; #服务名
}
① 监听端口一般都为http端口:80;可以修改为其他,这里修改为8088。

② server_name :默认为localhost ,这里修改为服务器ip地址。

(3)负载均衡列表基本配置

#服务器集群
upstream mycluster{
#这里添加的是上面启动好的两台Tomcat服务器
server 192.168.159.128:18080 weight=1;
      server 192.168.159.128:28080 weight=2;
}

location /{
#将访问请求转向至服务器集群,mycluster和上面upstream mycluster 对应
proxy_pass http://mycluster; # 真实的客户端IP
proxy_set_header   X-Real-IP        $remote_addr;
# 请求头中Host信息
proxy_set_header   Host             $host;
# 代理路由信息,此处取IP有安全隐患
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
# 真实的用户访问协议
proxy_set_header   X-Forwarded-Proto $scheme;
}
① location / {}:负载均衡访问的请求,可以添加筛选,假如我们要对所有的jsp后缀的文件进行负载均衡时,可以这样写:location ~ .*.jsp$ {}

② proxy_pass:请求转向自定义的服务器列表,这里我们将请求都转向标识为http://mycluster 的负载均衡服务器列表;

③ 在负载均衡服务器列表的配置中,Server指令:指定服务器的ip地址,weight是权重,可以根据机器配置定义权重(如果某台服务器的硬件配置十分好,可以处理更多的请求,那么可以为其设置一个比较高的weight;而有一台的服务器的硬件配置比较差,那么可以将前一台的weight配置为weight=2,后一台差的配置为weight=1)。weigth参数表示权值,权值越高被访问到的几率越大;

(4)完整的配置文件示例

#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 mycluster{
server 192.168.159.128:18080 weight=1;
server 192.168.159.128:28080 weight=2;
}

server {
listen       8088;
server_name  192.168.159.128;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
#将访问请求转向至服务器集群,mycluster和上面upstream mycluster 对应
proxy_pass http://mycluster; # 真实的客户端IP
proxy_set_header   X-Real-IP        $remote_addr;
# 请求头中Host信息
proxy_set_header   Host             $host;
# 代理路由信息,此处取IP有安全隐患
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
# 真实的用户访问协议
proxy_set_header   X-Forwarded-Proto $scheme;
}

#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配置差不多就是上面这些内容,当然仅仅是最基础的配置。

六、在浏览器中进行负载均衡测试

在浏览器中输入 : http://192.168.159.128:8088


不断刷新浏览器,访问的地址一直在变化,可以看到负载均衡的效果已经实现。

七、小结

这篇文章通过Nginx反向代理实现了Tomcat服务器集群的负载均衡效果。从这个Demo中,我们可以简单地感受到反向代理为我们所做的事情,并体会到什么是负载均衡。当然这次只是简单的使用了一下Nginx,做了一些很简单的配置,后续会做一些针对负载均衡的优化配置,还有就是Session共享的问题,以及Nginx高可用的问题,这些知识后续博客会介绍。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息