Nginx+Tomcat实现负载均衡集群

  |   0 评论   |   962 浏览

1.前期准备工作

1.1 安装

Nginx安装

Tomcat安装

1.2 copy两份tomcat

[root@localhost tomcat]# mkdir /usr/local/tomcat/tomcat9001
[root@localhost tomcat]# mkdir /usr/local/tomcat/tomcat9002
[root@localhost tomcat]# cp -Rf /usr/local/tomcat/apache-tomcat-8.0.49/* /usr/local/tomcat/tomcat9002
[root@localhost tomcat]# cp -Rf /usr/local/tomcat/apache-tomcat-8.0.49/* /usr/local/tomcat/tomcat9001

2.修改tomcat端口

[root@localhost tomcat]# vi tomcat9001/conf/server.xml 

<Server port="18005" shutdown="SHUTDOWN">
//9001tomcat端口号修改为9001
<Connector port="9001" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
//ajp连接器端口修改
<Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />
---------------------------------------------------------------------------------------------------
[root@localhost tomcat]# vi tomcat9002/conf/server.xml 

<Server port="28005" shutdown="SHUTDOWN">
//9001tomcat端口号修改为9002
<Connector port="9002" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
//ajp连接器端口修改
<Connector port="28009" protocol="AJP/1.3" redirectPort="8443" />

2.2 测试两个tomcat是否能够启动成功

    可以看到两个tomcat都启动成功了。

2.3 测试页面

    接下来写两个测试页面用于区分。

index.html


页面1
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>我是tomcat9001</title>
    </head>

    <body>
        <p5>我是tomcat9001</p5>
    </body>

</html>
页面2
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>我是tomcat9002</title>
    </head>

    <body>
        <p5>我是tomcat9002</p5>
    </body>

</html>


3.配置nginx

    接下来配置nginx负载均衡,找到nginx配置文件。在配置文件中http{}里加入集群配置:

#服务器的集群
upstream  192.168.2.160 {  #服务器集群名字
      server    192.168.2.160:9001  weight=1; 
      server    192.168.2.160:9002  weight=2; 
}

    服务器配置   weight是权重的意思,权重越大,分配的概率越高。

    在server{}里加入:

location / {
            proxy_pass http://192.168.2.160; #要与集群名相同     
            proxy_redirect default;
}  

    完整配置:

user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
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';
  
#charset gb2312;
     
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
     
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on; 
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  #limit_zone crawler $binary_remote_addr 10m;
   #服务器的集群  
    upstream  192.168.2.160 {  #服务器集群名字   
        server    192.168.2.160:9001  weight=1;  
        server    192.168.2.160:9002  weight=2;  
    }    
 #下面是server虚拟主机的配置
 server
  {
    listen 80;#监听端口
    server_name localhost;#域名

location / {  
            proxy_pass http://192.168.2.160;  #与集群名相同
            proxy_redirect default;  
        }  

    index index.html index.htm index.php;
    root /usr/local/nginx/html;#站点目录
      location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
    }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
    }
    access_log off;
  }

}

    配置完毕,开始测试。

3.1 测试,启动nginx

[root@localhost conf]# /usr/local/nginx/sbin/nginx 

    启动完毕可以访问一下index页面,多刷新几次就会发现,权重比相对较高的节点,被访问的概率会更高。

到此完毕。

这个家伙很懒,连签名都不写。

评论

发表评论