目录

[TOC]


场景

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

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

应用使用tomcat部署,目前没有域名,都是使用IP在局域网中单独访问。因为没有单独的服务器可以用来部署Nginx,所以Nginx部署在229服务器上。

安装依赖包

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

  • 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

其它依赖包

  • 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验证

其它依赖包安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[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/目录,编译安装

1
2
3
4
5
[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
主要配置点:
1、upstream
这里配置一组被代理的服务器地址

1
2
3
4
5
6
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、server

1
2
3
4
5
6
7
8
9
10
11
12
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://

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
[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

验证配置:./nginx -t

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

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

推荐学习:Nginx部署与配置