linux下tomcat+nginx实现集群和负载均衡

一、设备应用规划:

192.168.10.101  tomcat,处理动态页

192.168.10.104  tomcat,处理动态页

192.168.10.106  nginx  处理静态页  

 

二、查看pcre安装情况,如果未安装,先安装pcre

 

已经安装的情况

[root@localhostpcre-8.32]# rpm -q pcre

pcre-7.8-6.el6.x86_64

未安装的情况

tar -zxvfpcre-8.32.tar.gz

cd pcre-8.32

./configure--prefix=/opt/pcre

make

make install

 

 

备注:

报错:

configure:error: You need a C++ compiler for C++ support

处理:

yum install -ygcc gcc-c++

 

 

 

 

 

 

 

 

三、安装nginx

tar -zxvfnginx-1.4.4.tar.gz

cd nginx-1.4.4

./configure--prefix=/opt/nginx  --with-http_ssl_module--with-http_gzip_static_module --with-http_stub_status_module

 

 

报错

./configure:error: the HTTP rewrite module requires the PCRE library.

You can eitherdisable the module by using --without-http_rewrite_module

option, orinstall the PCRE library into the system, or build the PCRE library

statically fromthe source with nginx by using --with-pcre=<path> option.

 

解决方法:

yum  -y install pcre-devel

 

 

报错

./configure:error: SSL modules require the OpenSSL library.

You can eitherdo not enable the modules, or install the OpenSSL library

into the system,or build the OpenSSL library statically from the source

with nginx byusing --with-openssl=<path> option.

 

 

解决方法:

查看openssl安装情况

[root@localhostnginx-1.4.4]# rpm -q openssl

openssl-1.0.1e-15.el6.x86_64

 

yum -y installopenssl-devel

 

 

 

安装成功:

onfigurationsummary

  + using system PCRE library

  + using system OpenSSL library

  + md5: using OpenSSL library

  + sha1: using OpenSSL library

  + using system zlib library

 

  nginx path prefix: "/opt/nginx"

  nginx binary file:"/opt/nginx/sbin/nginx"

  nginx configuration prefix:"/opt/nginx/conf"

  nginx configuration file:"/opt/nginx/conf/nginx.conf"

  nginx pid file:"/opt/nginx/logs/nginx.pid"

  nginx error log file: "/opt/nginx/logs/error.log"

  nginx http access log file:"/opt/nginx/logs/access.log"

  nginx http client request body temporaryfiles: "client_body_temp"

  nginx http proxy temporary files:"proxy_temp"

  nginx http fastcgi temporary files:"fastcgi_temp"

  nginx http uwsgi temporary files:"uwsgi_temp"

  nginx http scgi temporary files:"scgi_temp"

 

 

 

make

make install

 

四、修改当前打开的最大文件数

ulimit -n 65535

 

vi/etc/security/limits.conf

添加两行

* soft nofile65535

* hard nofile65535

vi/etc/pam.d/login

添加一行

session    required    pam_limits.so

 

五、配置nginx

vi/opt/nginx/conf

 

user nobody ;

worker_processes  2;

 

error_log  logs/error.log error;

 

 

pid        logs/nginx.pid;

 

#一个nginx进程打开的文件描述符的上限

worker_rlimit_nofile65535;

 

events {

    use epoll;

    #一个进程允许的最大同时连接数

    worker_connections  65535;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 #负载均衡设置,ttt为随意设置的名称

    upstream ttt{   

    ip_hash;

    server 192.168.10.101:80;

    server 192.168.10.104:80;

    }

    

        #access_log  logs/access.log  main;

    client_max_body_size 20m;

    client_header_buffer_size   32k;

    large_client_header_buffers 4 32k;

    sendfile        on;

    tcp_nopush     on;

    tcp_nodelay    on;

    #keepalive_timeout  0;

    keepalive_timeout  60;

    client_header_timeout   10;

    client_body_timeout     10;

    send_timeout       10;

 

    #gzip on;

                fastcgi_intercept_errors on;

 

 

 

    server {

        #80端口接收用户的请求

        listen       80;

      #chinavvv2为nginx所在主机的主机名

        server_name  192.168.10.106;

        index index.jsp index.html;

        root /opt/webapps/ROOT;

        charset UTF-8;

 

        #access_log  logs/host.access.log  main;

        #location /image/{

        # alias  /opt/webapps/ROOT/image/;

        #}

        #location /images/{

    #     alias  /opt/webapps/ROOT/images/;

    #   }

        #location /sitegroup/

        #{

        #  root /opt/webapps/ROOT/;

        #}

 

    location ~ ^/(WEB-INF)/

        {

          deny all;

        }

   location ~\.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)${

             root /opt/webapps/ROOT;

     expires 24h;

        }

 

        #location ~*\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$

    #{

        #      valid_referers none blocked 127.0.0.1;

        #      if ($invalid_referer)

        #      {

        #      rewrite ^/ /images/error.gif;

                #return 403;

        #      }

        #}

      location ~ (\.jsp)|(\.do)$ {

      #location / {

           #访问地址设置为ttt,与上面的一致

            proxy_pass http://ttt;

            proxy_next_upstream http_500http_502 http_503 error timeout invalid_header;

            include /opt/nginx/conf/proxy.conf;

        }

 

        error_page  404              /404/404.html;

 

        # redirect server error pages to thestatic page /50x.html

        #

        error_page  500 502 503 504  /404.html;

        location = /404.html {

            root   /opt/webapps/ROOT/404/;

        }

 

 

 

    }

 

 

   

 

}

 

 

 

上传proxy.conf文件到/opt/nginx/conf/路径下

 

 

 

六、开启、停止、重启nginx

 

测试nginx是否安装成功

 /opt/nginx/sbin/nginx -t

 

返回结果:

nginx: theconfiguration file /opt/nginx/conf/nginx.conf syntax is ok

nginx:configuration file /opt/nginx/conf/nginx.conf test is successful

 

 

开启nginx

 /opt/nginx/sbin/nginx

 

 

ps -ef|grep -vgrep|grep nginx

root     19138    1  0 11:39 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

nobody   19139 19138 0 11:39 ?        00:00:00 nginx:worker process

 

停止nginx

 

从容停止Nginx:

kill -QUIT 主进程号

快速停止Nginx:

kill -TERM 主进程号

强制停止Nginx:

pkill -9 nginx

 

 

修改配置文件nginx.conf后,平滑重启nginx

kill -HUP 主进程号

 

举例:

[root@localhostROOT]# ps -ef|grep -v grep|grep nginx

root     18124    1  0 16:07 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

nobody   18125 18124 0 16:07 ?        00:00:00 nginx:worker process

nobody   18126 18124 0 16:07 ?        00:00:00 nginx:worker process

 

[root@localhostROOT]# kill -HUP  18124

[root@localhostROOT]# ps -ef|grep -v grep|grep nginx

root     18124    1  0 16:07 ?        00:00:00 nginx: master process/opt/nginx/sbin/nginx

nobody   18235 18124 0 16:48 ?        00:00:00 nginx:worker process

nobody   18236 18124 0 16:48 ?        00:00:00 nginx:worker process

 

 

添加nginx开机自启动

 

 

 

 

七、发布应用程序

192.168.10.101和192.168.10.104分别发布程序到/opt/webapps/webapps,修改数据库链接池

启动tomcat

192.168.10.106发布程序到/opt/webapps/webapps

 

 

 

 

八、测试集群

1.首先访问http://192.168.10.106

2.关闭192.168.10.101上的tomcat

3.访问动态页是否正常

 

 

九、测试负载均衡

 

1.访问http://192.168.10.106,查看在线会话数

 

[root@localhost~]# netstat -an|grep 80|wc -l

10

 

说明静态页由192.168.10.106来处理

 

2.访问动态页正常,

在192.168.10.101查看在线会话数

[root@localhostbin]# netstat -an|grep 80|wc -l

10

在192.168.10.104查看在线会话数

[root@localhostbin]# netstat -an|grep 80|wc -l

13

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值