Centos 7离线安装Nginx 配置负载均衡集群

场景

项目中有三台应用服务器,系统为Centos 7 ,应用地址分别为:html

  • 192.168.198.229:8080
  • 192.168.198.230:8080
  • 192.168.198.231:8080

应用使用tomcat部署,目前没有域名,都是使用IP在局域网中单独访问。由于没有单独的服务器能够用来部署Nginx,因此Nginx部署在229服务器上。nginx

安装依赖包

在安装Nginx前,须要先安装好一些依赖包。
gcc依赖包centos

  • gcc-4.8.5-16.el7.x86_64.rpm
  • glibc-devel-2.17-196.el7.x86_64.rpm
  • glibc-headers-2.17-196.el7.x86_64.rpm
  • kernel-headers-3.10.0-693.el7.x86_64.rpm

其它依赖包tomcat

  • pcre-devel-8.32-17.el7.x86_64.rpm
  • zlib-devel-1.2.7-17.el7.x86_64.rpm
  • openssl-fips-2.0.10.tar.gz

由于没法使用yum,我下载好后经过ftp上传到服务器。依赖包下载传送门:https://centos.pkgs.org/
前四个为gcc安装包与相关依赖,最后一个openssl-fips若是使用rpm,还须要安装不少依赖包,所以使用压缩包安装更简单。
gcc安装
gcc安装
gcc安装验证:
gcc验证
服务器

其它依赖包安装学习

[root@APP1 opt]# rpm -ivh pcre-devel-8.32-17.el7.x86_64.rpm 
警告:pcre-devel-8.32-17.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...

[root@APP1 opt]# rpm -ivh zlib-devel-1.2.7-17.el7.x86_64.rpm 
警告:zlib-devel-1.2.7-17.el7.x86_64.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID f4a80eb5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:zlib-devel-1.2.7-17.el7          ################################# [100%]
   
[root@APP1 opt]# tar zxvf openssl-fips-2.0.10.tar.gz 
[root@APP1 opt]# cd openssl-fips-2.0.10/
[root@APP1 openssl-fips-2.0.10]# ./config && make && make install

安装Nginx

安装好上述依赖包后就能够安装Nginx了。安装以下:
使用tar将nginx-1.12.0.tar.gz 解压到 /usr/local/目录,编译安装测试

[root@HMDMAPP1 opt]# tar -zxvf nginx-1.12.0.tar.gz -C /usr/local/
[root@HMDMAPP1 opt]# cd /usr/local/nginx-1.12.0/
[root@HMDMAPP1 nginx-1.12.0]# ./configure && make && make install
[root@HMDMAPP1 nginx-1.12.0]# whereis nginx
nginx: /usr/local/nginx

配置Nginx

安装好后咱们须要对Nginx进行配置。
配置文件路径为:/usr/local/nginx/sconf/nginx.conf
主要配置点:
一、upstream
这里配置一组被代理的服务器地址spa

upstream mysvr {
        server 192.168.198.229:8080   weight=1 max_fails=3 fail_timeout=15;
        server 192.168.198.230:8080   weight=1 max_fails=3 fail_timeout=15;
        server 192.168.198.231:8080   weight=1 max_fails=3 fail_timeout=15;
    }

二、server.net

server {
        listen       80; #监听端口,与应用端口不一样
        server_name  192.168.198.229; #监听地址,通常是配置域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://mysvr; #请求转向upstream配置中mysvr定义的服务器列表
        }
}
请求转向还有另一种写法:
若是upstream 中的服务器列表地址前加了 http:// 则在server中的请求转向地址 mysvr不须要加 http://
upstream mysvr{
    server http://192.168.198.229:8080   weight=1 max_fails=3 fail_timeout=15;
       ...
       ...
}
server{
    ....
    location / {
            proxy_pass mysvr; #请求转向upstream配置中mysvr定义的服务器列表
        }
}

启动Nginx

[root@HMDMAPP1 /]# cd /usr/local/nginx/sbin
[root@HMDMAPP1 sbin]# ./nginx
Nginx经常使用命令
查看进程: ps -aux |grep 'nginx'
重启nginx: ./nginx -s reopen
中止nginx: ./nginx -s stop
从新载入配置文件: ./nginx -s reload

经过 192.168.198.229+应用地址 进行访问,咱们能够在不一样的服务器中的页面中添加标识来测试Nginx配置是否成功。下面访问test3.html页面不一样刷新显示结果以下:
这里写图片描述
这里写图片描述
这里写图片描述
能够看到访问地址没有变化,但Nginx把请求分配到了不一样的服务器上。3d

本文中使用到了依赖包与Nginx.conf完整配置文件下载:https://download.csdn.net/dow...

推荐学习:Nginx部署与配置

相关文章
相关标签/搜索