场景项目中有三台应用服务器,系统为Centos 7 ,应用地址分别为:192.168.198.229:8080192.168.198.230:8080192.168.198.231:8080应用使用tomcat部署,目前没有域名,都是使用IP在局域网中单独访问。因为没有单独的服务器可以用来部署Nginx,所以Nginx部署在229服务器上。安装依赖包在安装Nginx前,需要先安装好一些依赖包。gcc依赖包gcc-4.8.5-16.el7.x86_64.rpmglibc-devel-2.17-196.el7.x86_64.rpmglibc-headers-2.17-196.el7.x86_64.rpmkernel-headers-3.10.0-693.el7.x86_64.rpm其它依赖包pcre-devel-8.32-17.el7.x86_64.rpmzlib-devel-1.2.7-17.el7.x86_64.rpmopenssl-fips-2.0.10.tar.gz因为无法使用yum,我下载好后通过ftp上传到服务器。依赖包下载传送门:https://centos.pkgs.org/前四个为gcc安装包与相关依赖,最后一个openssl-fips如果使用rpm,还需要安装很多依赖包,因此使用压缩包安装更简单。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 nginxnginx: /usr/local/nginx配置Nginx安装好后我们需要对Nginx进行配置。配置文件路径为:/usr/local/nginx/sconf/nginx.conf主要配置点:1、upstream这里配置一组被代理的服务器地址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; } 2、serverserver { 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]# ./nginxNginx常用命令查看进程: ps -aux |grep ’nginx’重启nginx: ./nginx -s reopen 停止nginx: ./nginx -s stop重新载入配置文件: ./nginx -s reload通过 192.168.198.229+应用地址 进行访问,我们可以在不同的服务器中的页面中添加标识来测试Nginx配置是否成功。下面访问test3.html页面不同刷新显示结果如下:可以看到访问地址没有变化,但Nginx把请求分配到了不同的服务器上。本文中使用到了依赖包与Nginx.conf完整配置文件下载:https://download.csdn.net/dow…推荐学习:Nginx部署与配置