nginx集群

已有环境

  1. centos7
  2. nginx服务器
  3. 多台tomcat服务器

nginx配置文件

nginx配置文件是conf目录下的nginx.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
upstream tomcats {
server 192.168.1.241:8080 weight=5;
server 192.168.1.241:9090 weight=5;
server 192.168.1.242:8080 weight=5;
}
server {
listen 80;
server_name 192.168.1.241;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://tomcats;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

说明:

  1. upstream tomcats中表示tomcat服务器的ip和端口。
  2. weight表示的权重,数越大,权重越高,分配到的机会越大。
  3. proxy_pass对应的是集群名称tomcats。

测试

  1. 将tomcat默认页面修改成各不相同的页面。
  2. 同台机器中的多个tomcat还需修改其端口号,默认的8080,8005,8009均要修改。
  3. 启动tomcat。
  4. 重启nginx。
  5. 访问nginx不停刷新,不同的页面轮流显示表示nginx集群配置成功。