CentOS+Nginx+Tomcat集群及负载均衡

来源:互联网 发布:nokia5230软件下载 编辑:程序博客网 时间:2024/05/21 06:24

前两天申请了一台免费腾讯云服务器,只有5天时间,不知道该怎么用呢,后来想到自己用来学习搭建一个nginx+tomcat集群测试吧,正好自己也有一个域名,就绑定到这台云服务器上的IP地址。好了,言归正传,我把自己的安装过程记下笔记。

一.安装Nginx

安装正则

yum -y install pcre*

安装openssl

yum -y install openssl*

下载nginx

wgethttp://nginx.org/download/nginx-1.7.8.tar.gz

解压缩

tar -zxvf nginx-1.7.8.tar.gz

配置,注意/usr/local/nginx必须存在

./configure --prefix=/usr/local/nginx--with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module--with-pcre

编译

make

安装

make install

检查nginx是否正确安装

cd /usr/local/nginx/sbin

./nginx -t

nginx: the configuration file/usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file/usr/local/nginx/conf/nginx.conf test is successful

启动

/usr/local/nginx-1.7.8/sbin/nginx

访问


二.Tomcat安装。
    Tomcat的下载安装就不再赘述了,大家都懂的。
Tomcat安装两个,分别是
tomcat-A   127.0.0.1:8080
tomcat-B   127.0.0.1:9080
编写一个home.html文件,放在tomcat-A/webapps/ROOT目录里,内容如下:

<html>  <head>    <title>tomcat负载均衡</title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>  </head>  <body><h1>这是个测试页面,现在请求的页面在tomcat-A</h1>  </body></html>

再编写一个home.html文件,放在tomcat-B/webapps/ROOT目录里,内容如下:

<html>  <head>    <title>tomcat负载均衡</title>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>  </head>  <body><h1>这是个测试页面,现在请求的页面在tomcat-B</h1>  </body></html>

访问tomcat-A


访问tomcat-B


两个tomcat正常运行。

 

三.集群和负载均衡配置

现在配置负载均衡

修改配置文件

cd /usr/local/nginx/conf

vi nginx.conf

在http里增加,这是负载均衡配置

   upstream www {    ip_hash;       server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;    server 127.0.0.1:9080 weight=1 max_fails=2 fail_timeout=30s;}

说明:

ip_hash是根据ip进行hash值分发,同一个ip客户端只分发到一个tomcat里。

weight 权重:分发的数量比重

在server里增加

   location / {          proxy_next_upstream http_502 http_504 error timeout invalid_header;       proxy_set_header Host  $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;       proxy_pass http://www;        expires      3d;    }
整个配置文件如下:

#user  nobody;worker_processes  2;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    use epoll;    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 www {    ip_hash;    server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;    server 127.0.0.1:9080 weight=1 max_fails=2 fail_timeout=30s;    }    server {        listen       80;        server_name  localhost;        root   html;        index  index.html index.htm;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {      proxy_next_upstream http_502 http_504 error timeout invalid_header;           proxy_set_header Host  $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           proxy_pass http://www;    expires      3d;    }         #location / {        #    root   html;        #    index  index.html index.htm;        #}        #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;        }    }}
重新加载配置文件
/usr/local/nginx-1.7.8/sbin/nginx –s reload
启动
/usr/local/nginx-1.7.8/sbin/nginx
本机访问

本机访问的是tomcat-B,由于该机没有ui,所以只能使用curl命令测试

在客户端(自己的电脑)访问

这个访问的是tomcat-A,不管你怎么刷新浏览器都是访问tomcat-A

测试故障转移
把tomcat-A kill

再刷新客户端时,则访问tomcat-B

再启动tomcat-A
/usr/local/tomcat-A/bin/startup.sh

再刷新客户端,又访问tomcat-A了。


至此,完。




0 0