荣耀彩票代理

IT技术互动交流平台

Nginx+Tomcat反向代理负载均衡集群部署指南

来源:IT165收集  发布日期:2016-12-14 20:33:02

ZHUANZAIQINGZHUMINGCHUCHU:http://blog.csdn.net/smartbetter/article/details/53535435

NginxSHIYIZHONGFUWUQIRUANJIAN,YESHIYIZHONGGAOXINGNENGDEhttpHEFANXIANGDAILIFUWUQI,TONGSHIHAISHIYIGEDAILIYOUJIANFUWUQI。YEJIUSHISHUO,WOMENZAINginxSHANGKEYIFABUWANGZHAN,KEYISHIXIANFUZAIJUNHENG(TIGAOYINGDAXIAOLV,BIMIANFUWUQIBENGKUI),HAIKEYIZUOWEIYOUJIANFUWUQISHIXIANSHOUFAYOUJIANDENGGONGNENG。ERZUICHANGJIANDEJIUSHISHIYONGNginxSHIXIANFUZAIJUNHENG。

荣耀彩票代理NginxYUQITAFUWUQIDEXINGNENGBIJIAO:

Tomcat服务器面向Java语言,是重量级的服务器,而Nginx是轻量级的服务器。Apache服务器稳定、开源、跨平台,但是Apache服务器不支持高并发,Nginx能支持处理百万级的TCP连接,10万以上的并发连接,并且是一个很好的跨平台服务器。
荣耀彩票代理 Nginx主要优点有可以实现高并发、部署简单、内存消耗少、成本低等,主要缺点有rewrite功能不够强大,模块没有Apache的多。

荣耀彩票代理BENPIANZHUYAOJIANGJIE Nginx + Tomcat FANXIANGDAILIHEFUZAIJUNHENGDEBUSHU,YITONGSUSHIYONGWEIZHU。BENPIANWENZHANGMEIGEBUFENZHIJIANMEIYOUTAIDAGUANXI,KEGENJUXUQIUFENKAIXUEXI。

下来看一下Nginx反向代理的过程:
Nginx反向代理的过程

NginxFUZAIJUNHENGDEGUOCHENG(HUIZIDONGXUANZEYALIJIAOXIAODEFUWUQIJINXINGFANGWEN):

Nginx负载均衡的过程

可以看出,负载均衡是通过反向代理的原理实现的,所以也称 反向代理的负载均衡 。所以我们会部署负载均衡,那么反向代理也就会了。

ZONGDELAISHUO,FUZAIJUNHENGSHIXIANDEFANGSHIFENWEIRUANJIANSHIXIANHEYINGJIANSHIXIANLIANGZHONG,YINGJIANSHIXIANYUNXINGDEXIAOLVFEICHANGGAO,DANSHIDUIYINGDECHENGBENYEFEICHANGGAO。RUANJIANSHIXIANYUNXINGXIAOLVBURUYINGJIAN,DANSHICHENGBENXIANGDUILAISHUODIDEDUO。ERSHIYONGNginxFUWUQISHIXIANFUZAIJUNHENG,NEIMEJIUSHITONGGUORUANJIANDEFANGSHILAISHIXIANFUZAIJUNHENG,BINGQIENginxBENSHENZHICHIGAOBINGFADENG。GUERSHIYONGNginxFUWUQISHIXIANFUZAIJUNHENG,NENGDADAJIEYUEQIYEDECHENGBEN,BINGQIEYOUYUNginxSHIFUWUQIRUANJIAN,QIZHIXINGXIAOLVYESHIFEICHANGGAO。BENPIANDEMUDEYEJIUSHIBANGZHUDAJIASHIYONGNginxSHIXIANFUZAIJUNHENG。

FUZAIJUNHENGDEHEXINJIUSHIJIANLIYIGEFUWUQIJIQUN,RANHOUYONGHUSHOUXIANFANGWENDAODISANFANGDAILIFUWUQI(ZHEILIWOMENXUANYONGNginx),RANHOUYOUDAILIFUWUQIXUANZEYIGEJIQUNZHONGDEFUWUQI,RANHOUJIANGQINGQIUYINRUXUANDINGDEFUWUQI(ZHEILIWOMENXUANYONGTomcat)。

如下图,通过反向代理我们实现下面的负载均衡,这里我们 假定 四台服务器公网的ip,一台做代理服务器,三台做负载均衡下的服务器:

具体实现负载均衡

HAOLE,ZHENGTIJIAGOUYIJINGJIBENSHANGQINGXILE,XIALAIWOMENLAIJUTISHIXIANXIA:

TISHI:BENPIANWOMENJIBENSHANGDOUSHISHIYONGSSHJINXINGXIANGGUANCAOZUODE,WindowsXIAKEYICHANGSHIANZHUANGPuTTY,MacXIAZHIJIESHIYONGXITONGZIDAIDEZHONGDUANGONGJUJIKE。

1.Linux下搭建Nginx服务器

WOMENZAI 192.168.2.20 ZHEITAIFUWUQISHANGDAJIANNginxFUWUQI:

荣耀彩票代理1)XIAZAINginx http://nginx.org/

2)SHANGCHUANFUWUQINginxANZHUANGBAO

$ scp ~/Downloads/nginx-1.10.2.tar.gz root@192.168.2.20:/usr/local

3)ANZHUANGNginx

$ ssh root@192.168.2.20                          //SSH连接
# yum -y install gcc gcc-c++ autoconf automake   //gcc、gcc-c++的库文件
# yum install -y pcre pcre-devel                 //安装Nginx依赖包
# yum install -y zlib zlib-devel

荣耀彩票代理ZHUYI:-yBIAOSHIYUDAOPANDUANQUANBUyes,autoconfBIAOSHIZIDONGPEIZHI,automakeBIAOSHIZIDONGBIANYI。

# cd /usr/local
# tar -zxvf nginx-1.10.2.tar.gz                 //解压缩
# cd nginx-1.10.2                               //切换到该目录下
# ./configure                                   //配置
# make
# make install                                  //安装

JIANYANSHIFOUWANCHENGANZHUANG:

# cd /usr/local
# ls                                            //如果存在nginx文件夹,则安装成功

JINGGUOSHANGMIANDEANZHUANGBUZHOUHEMULUSHEZHI,nginxDEQIDONGCHENGXUJIUSHI/usr/local/nginx/sbin/nginx,MORENPEIZHIWENJIANSHI/usr/local/nginx/conf/nginx.conf,DANSHIBUJIANYIZHIJIEBIANJInginx.conf,YIBANWOMENDOUXUANZEXINJIANPEIZHIWENJIAN,RANHOUZAIXINJIANDEPEIZHIWENJIANZHONGXIUGAIDUANKOU、FANXIANGDAILILUJINGDENG。

2.Nginx的启动、停止、信号控制

1)QIDONGNginxFUWUQI(GESHI:NginxKEZHIXINGWENJIAN -c NginxPEIZHIWENJIAN):

# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2)TINGZHINginxFUWUQI:

TINGZHINginxFUWUQISHOUXIANYAOCHAXUNNginxDEZHUJINCHENGHAO(master process),JIASHECICHUCHAXUNDEDAO1060(FANGBIANXIAMIANYANSHISHIYONG):

# ps -ef|grep nginx

XIALAIKANRUHETINGZHINginxFUWUQI,TINGZHINginxYOUSANZHONGFANGSHI:

CONGRONGTINGZHI:

# ps -ef|grep nginx    //查看Nginx的主进程号(master process),假设此处查询得到1060
# kill -quit 1060

KUAISUTINGZHI:

# kill -term 1060

QIANGZHITINGZHI:

# pkill -9 nginx

3)ZHONGQINginxFUWUQI:

DANGWOMENXIUGAILENginxPEIZHIWENJIAN,XUYAOZHONGQICAINENGSHENGXIAO。WOMENZAIZHONGQIZHIQIANHAIXUYAOYANZHENGPEIZHIWENJIANDEZHENGQUEXING,RANHOUJINXINGZHONGQICAOZUO:

# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf      //验证
# /usr/local/nginx/sbin/nginx -s reload                                   //重启

4)PINGHUASHENGJINginxFUWUQI

PINGHUASHENGJIBUHUITINGDIAOZAIYUNXINGZHEDEJINCHENG,ZHEIXIEJINCHENGHUIJIXUCHULIQINGQIU,DANBUHUIZAIJIESHOUXINQINGQIU,ZAIZHEIXIELAOJINCHENGZAICHULIWANHAIZAICHULIDEQINGQIUHOU,TINGZHI。CIPINGHUASHENGJIDEGUOCHENGZHONG,XINKAIDEJINCHENGHUIBEICHULI。ZHEIJIUSHIPINGHUASHENGJI。

# /usr/local/nginx/sbin/nginx -v             //查看当前版本

XIAMIANJINXINGPINGHUASHENGJI:

# cd /usr/local
# tar -zxvf nginx-1.11.6.tar.gz              //解压缩 新版本Nginx
# cd nginx-1.11.6                            //切换到该目录下
# ./configure                                //配置
# make
# cd /usr/local/nginx/sbin                   //打开 旧版本Nginx可执行文件位置
# cp nginx nginx.old                  //备份 旧版本Nginx可执行文件,防止升级错误而无法恢复
# cp -rfp /usr/local/nginx-1.11.6/objs/nginx /usr/local/nginx/sbin     //复制新版本可执行文件到旧版本处
# rm -f /usr/local/nginx-1.11.6.tar.gz       //善后工作 删除压缩文件
# rm -rf /usr/local/nginx-1.11.6             //删除文件夹

ZHICI,NginxFUWUQIPINGHUASHENGJICHENGGONG。

3.Nginx中负载均衡的实现

荣耀彩票代理WOMENHAISHIXIANLIANJIESSH,RANHOUJINXINGXIAMIANCAOZUO(YIBANBUJIANYIXIUGAIMORENZHUPEIZHIWENJIANnginx.conf,SUOYIWOMENXINJIANFUZAIJUNHENGPEIZHIWENJIANfzjh.conf,BAOZHENGFUWUQIANQUAN,RUXIA):

# cd /usr/local/nginx/conf
# touch fzjh.conf
# vi fzjh.conf    //用vi编辑器打开文件,然后按键盘的i

注意:vi编辑器中,键盘按 i 进入 INSERT 状态,按 Esc 退出 INSERT 状态。
荣耀彩票代理 然后输入以下配置代码(注释部分按需开启):

#设置低权限用户,为了安全而设置的
user nobody;

#工作衍生进程数
worker_processes 4;

#设置错误文件存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#设置pid存放路径(pid是控制系统中重要文件)
#pid logs/nginx.pid;

#设置最大连接数
events{
    worker_connections 1024;
}

http{
    #主要是用于设置一组可以在proxy_pass和fastcgi_pass指令中使用额代理服务器,默认负载均衡方式为轮询
    upstream tomcat_client {
        #设置同一个cookie的两次/多次请求,请求的是同一台服务器
        ip_hash;
        #weight权重,默认1,权重越大访问概率越大,backup备用服务器,服务器全部崩溃后启动
        server 192.168.2.21:8080 weight=5;
        server 192.168.2.22:8080 weight=5;
        server 192.168.2.23:8080 weight=5 backup;
    }

    #开启gzip压缩,开启后,访问网页会自动压缩
    #gzip on;

    #指定服务器的名称和参数
    server {
        listen 80;
        server_name  test.nginxtest.net;

        #设置字符
        #charset koi8-r;

        #location / 指用根目录做负载均衡
        location / {
            proxy_pass http://tomcat_client;
            proxy_redirect default;
            #设置代理
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

荣耀彩票代理SHURUWANCHENGHOUANXIAEsc,RANHOUSHURU:

:wq!

JIUKEYIBAOCUNBINGTUICHUFUZAIJUNHENGDEPEIZHIWENJIANLE,XIALAIWOMENJIAZAIWOMENDEPEIZHIWENJIAN:

# /usr/local/nginx/sbin/nginx                                           //启动Nginx
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fzjh.conf        //加载配置文件

荣耀彩票代理RUGUOCUNZAIDUANKOUZHANYONG,KEYISHIYONGRUXIAMINGLINGKillDIAONginxDECHENGXU:

# killall -9 nginx

荣耀彩票代理ZHICI,NginxFUWUQIBUSHUWANBI。XIAMIANWOMENKAISHIBUSHUFUZAIJUNHENGXIADENEISANTAIZHENSHITIGONGSHUJUDEFUWUQI。

4.Linux下安装jdk

WOMENZAI192.168.2.21、192.168.2.22、192.168.2.23ZHEISANTAIFUWUQIANZHUANGjdk,YI192.168.2.21WEILI:

1)XIAZAIjdk http://www.oracle.com/technetwork/java/javase/downloads/index.html

2)SHANGCHUANFUWUQIjdkANZHUANGBAO

$ scp ~/Downloads/jdk-8u112-linux-x64.tar.gz root@192.168.2.21:/usr/local

3)ANZHUANGjdk

$ ssh root@192.168.2.21                 //SSH连接
# cd /usr/local
# tar -zxvf jdk-8u112-linux-x64.tar.gz  //解压缩
# mv jdk1.8.0_112/ jdk                  //将jdk1.8.0_112重命名为jdk
# vi /etc/profile                       //用vi编辑器打开文件,然后按键盘的i

荣耀彩票代理XIALAIWOMENYIDONGGUANGBIAOZHIZUIHOUMIAN,LIANGGEHUICHEHUANXING,RANHOUTIANJIAXIAMIANDAIMAPEIZHIjavaDEHUANJINGBIANLIANG:

JAVA_HOME='/usr/local/jdk'
CLASS_PATH='$JAVA_HOME/lib:$JAVA_HOME/jre/lib'
PATH='.:$PATH:$JAVA_HOME/bin'
CATALINA_HOME='/usr/local/tomcat'
export JAVA_HOME CATALINA_HOME

SHURUWANCHENGHOUANXIAEsc,RANHOUSHURU:

:wq!

荣耀彩票代理JIUKEYIBAOCUNBINGTUICHULE。CISHIWOMENXIANGRANGQILIJISHENGXIAO,ZEXUYAOJIXUSHURUMINGLING:

# source /etc/profile

CISHIprofileWENJIANJIUGENGXINLE,CISHIHUANJINGBIANLIANGJIUPEIZHICHENGGONGLE。XIAMIANWOMENYANZHENGYIXIAjdkSHIFOUANZHUANGPEIZHICHENGGONG:

# java -version

荣耀彩票代理RUGUOFANHUIjavaBANBENHAOZEANZHUANGCHENGGONG。

5.Linux下搭建Tomcat服务器

WOMENZAI192.168.2.21、192.168.2.22、192.168.2.23ZHEISANTAIFUWUQIDAJIANTomcatFUWUQI,YI192.168.2.21WEILI:

1)XIAZAITomcat http://tomcat.apache.org/

2)SHANGCHUANFUWUQITomcatANZHUANGBAO

$ scp ~/Downloads/apache-tomcat-8.5.9.tar.gz root@192.168.2.21:/usr/local

3)ANZHUANGTomcat

$ ssh root@192.168.2.21                 //SSH连接
# cd /usr/local
# tar -zxvf apache-tomcat-8.5.9.tar.gz  //解压缩
# mv apache-tomcat-8.5.9/ tomcat        //将apache-tomcat-8.5.9重命名为tomcat

荣耀彩票代理4)SHEZHITomcatWANGYEWENJIANMULU

荣耀彩票代理TomcatMORENWANGYEWENJIANDELUJINGSHI /usr/local/tomcat/webapps/ROOT,DANSHIYIBANWOMENBUZAIZHEILICUNFANG,WOMENZAILinuxGENMULUXINJIAN data/wwwroot MULU:

# mkdir /data                         //新建目录
# mkdir /data/www
# mkdir /data/www/ROOT
# cd /usr/local/tomcat/conf
# vi server.xml                       //用vi编辑器打开server.xml配置文件,打开后键盘按i

荣耀彩票代理WOMENZHAODAOHostJIEDIANXIADEappBase=”webapps”,XIUGAIWEI:

appBase='/data/www'

荣耀彩票代理SHURUWANCHENGHOUANXIAEsc,RANHOUSHURU:

:wq!

荣耀彩票代理JIUKEYIBAOCUNBINGTUICHUPEIZHIWENJIANLE,RANHOUZHONGQITomcatJIKESHENGXIAO,CISHITomcatDEWANGYEWENJIANMULUJIUBIANCHENGLE /data/www/ROOT LE。

PEIZHITomcatHUANJINGBIANLIANG,WOMENZAIPEIZHIjdkDESHIHOUYIJINGPEIZHIHAOLE,KEYIHUITOUKANYIXIA。

6.Tomcat的启动、停止

1)QIDONGTomcatFUWUQI

# /usr/local/tomcat/bin/startup.sh

荣耀彩票代理2)TINGZHITomcatFUWUQI:

# /usr/local/tomcat/bin/shutdown.sh

到此,Nginx + Tomcat 负载均衡 集群 已经部署完毕了。不过有了服务器,肯定也少不了数据库,下面拓展一下Linux下安装MySQL数据库的方法。

7.Linux下安装MySQL数据库

WOMENZHONGXINZHAOLE 192.168.2.30 ZHEITAIFUWUQISHANGMySQLSHUJUKU:

1)XIAZAIMySQLSHUJUKU http://dev.mysql.com/downloads/mysql/

2)SHANGCHUANMySQLSHUJUKUANZHUANGBAO

$ scp ~/Downloads/mysql-5.1.51.tar.gz root@192.168.2.30:/usr/local

3)ANZHUANGMySQL

$ ssh root@192.168.2.30                 //SSH连接
# groupadd mysql                        //建立mysql的组
# useradd mysql -g mysql                //增加用户mysql到mysql的组中

# yum list|grep ncurses
# yum -y install ncurses-devel
# yum install ncurses-devel

# cd /usr/local
# tar -zxvf mysql-5.1.51.tar.gz         //解压缩
# cd mysql-5.1.51

# ./configure --prefix=/usr/local/mysql --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-readline --with-sll 
//配置,设置安装路径,设置编译mysql不带共享库,设置编译client不带共享库,设置要以rmp的方式安装tar包,设置以rmp的方式安装opensll

# make
# make install                          //安装,在make[4]的时候等待时间比较长,几分钟到十几分钟,都是正常现象

# /usr/local/mysql/bin/mysql_install_db --user       //对安装好的MySQL进行初始化
# cp ./support-files/mysql.server /etc/init.d/mysql  //将MySQL启动服务复制到系统中并重命名为mysql
# cp ./support-files/my-medium.cnf /etc/my.cnf       //复制规则文件
# chmod 755 /etc/init.d/mysql                        //更改文件权限

//文件权限由三个数字组成,第一位:文件拥有者的权限,第二位:文件拥有者同组的人的权限,第三位:文件拥有者非同组人的权限
//7:可读可写可执行,5:可读可执行

# cd /usr/local/mysql
# chown -R mysql .                                   //将 /usr/local/mysql 的拥有者更改为mysql
# chgrp -R mysql .                                   //将 /usr/local/mysql 归到mysql组中

# ps -ef|grep mysql
# kill -9 3632                    //将mysql所有进程号依次杀死,这里假定是3632
# /usr/local/mysql/bin/mysql_install_db --user=mysql       //再次初始化
# service mysql start                                      //启动MySQL
# /usr/local/mysql/bin/mysqladmin -u root password '123456'  //设置MySQL密码

DAOCIMySQLJIUANZHUANGCHENGGONGLE,XIALAIWOMENCESHIYIXIA:

# /usr/local/mysql/bin/mysql -u root -p

荣耀彩票代理RUGUOSHURUMIMAHOUKEYIDENGJINMySQL,NEIMEJIUCESHICHENGGONG。XIAMIANWOMENSHEZHIMySQLYUNXUKAIQIYUANCHENGLIANJIE:

# /usr/local/mysql/bin/mysql -u root -p

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
//创建用户用于远程连接(root:用户名,%:所有电脑都可以连接,也可以设置某个ip地址运行连接,123456:密码)

mysql> flush privileges;            //立即生效

荣耀彩票代理XIAMIANWOMENCHAXUNYIXIASHUJUKUDEYONGHU:

mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
mysql> q                           //登出

荣耀彩票代理ZHAOYITAIDIANNAOCESHIYIXIA,SHIYONGNavicatYUANCHENGDENGLUMySQL,DENGJINCHENGGONG。

BENPIANWENZHANGDAOCIJIESHU。

Tip:XIHUANBENPIANWENZHANGDEJIDE“DINGYIXIA”E!

Tag标签:      
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规