Nginx负载均衡集群——反向代理企业案例

在这里插入图片描述
该架构图是企业常见的动静分离集群架构图,例如该网站域名是www.chaoge.com

  • 当用户请求www.chaoge.com/upload/xx,该形式的URL,代理服务器会将其转发到上游服务器upload_pools
  • 当用户访问www.chaoge.com/static/xx,该形式的URL,代理服务器会将其转发到静态服务器地址池static_pools
  • 当用户访问www.chaoge.com/xx,该形式的URL,也就是不包含指定的路径URL,代理服务器将其默认都转发给动态服务器池处理数据

1.节点服务器配置

  • web01 192.168.178.124 静态服务器池
  • web02 192.168.178.125 上传服务器池
  • web03 192.168.178.127 默认地址池,动态服务器池
  • lb01 192.168.178.126

2.检查各个机器的nginx安装情况

[root@lb01 conf]# which nginx
[root@lb01 conf]# rpm -qa |grep nginx

3.备份并修改lb01机器的nginx.conf
#备份nginx.conf

[root@lb01 conf]# mv nginx.conf nginx.conf.$(date +%F)
[root@lb01 conf]# cp nginx.conf.default nginx.conf

#修改nginx.conf,添加

#配置静态服务器地址池
upstream static_pools {
	server 192.168.178.124;	
    }

#配置上传服务器地址池
    upstream upload_pools {
	server 192.168.178.125;
    }

#配置默认服务器地址池、动态地址池
    upstream default_pools {
	server 192.168.178.127;
    } 



server {
	listen 80;
	server_name www.chaoge.com;

	location /static/  {
	proxy_pass http://static_pools;
	include proxy.conf;
	}

	location /upload/ {
	proxy_pass http://upload_pools;
	include proxy.conf;
	}

	location / {
	proxy_pass http://default_pools;
	include proxy.conf;
	}
}

4.语法检查

[root@lb01 conf]# nginx -t

5.启动nginx

[root@lb01 conf]# nginx

6.配置web01服务器节点(静态服务器节点)
#备份nginx.conf文件

[root@web01 opt]# mv /opt/nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf$(date +%F)
[root@web01 opt]# cp /opt/nginx/conf/nginx.conf.default /opt/nginx/conf/nginx.conf

#修改nginx

server {
listen 80;
server_name www.chaoge.com;
#让nginx支持中文
charset utf-8;
location / {
        root html/www;
        index index.html index.htm;
}
access_log logs/access_www.log;
}

#创建文件

[root@web01 logs]# mkdir -p /opt/nginx/html/www/static/
[root@web01 logs]# echo "我是超哥配置的静态服务器static" > 

/opt/nginx/html/www/static/index.html
#重启nginx

[root@web01 opt]# nginx -s reload

7.配置web02服务器节点(上传服务器节点)
#备份nginx.conf文件

[root@web02 opt]# mv /opt/nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf$(date +%F)
[root@web02 opt]# cp /opt/nginx/conf/nginx.conf.default /opt/nginx/conf/nginx.conf

#修改nginx

server {
listen 80;
server_name www.chaoge.com;
#让nginx支持中文
charset utf-8;
location / {
        root html/www;
        index index.html index.htm;
}
access_log logs/access_www.log;
}

#创建文件

[root@web02 nginx-1.16.0]# mkdir -p /opt/nginx/html/www/upload/
[root@web02 nginx-1.16.0]# echo "我是超哥配置的uploads服务器" > /opt/nginx/html/www/upload/index.html

#重启nginx

[root@web02 opt]# nginx -s reload

8.配置web03服务器节点(默认动态服务器节点)
#备份nginx.conf文件

[root@web03 opt]# mv /opt/nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf$(date +%F)
[root@web03 opt]# cp /opt/nginx/conf/nginx.conf.default /opt/nginx/conf/nginx.conf

#修改nginx

server {
listen 80;
server_name www.chaoge.com;
#让nginx支持中文
charset utf-8;
location / {
        root html/www;
        index index.html index.htm;
}
access_log logs/access_www.log;
}

#生成首页文件

[root@web03 nginx-1.16.0]# mkdir -p /opt/nginx/html/www/
[root@web03 nginx-1.16.0]# echo "我是josen配置的默认动态服务器"  > /opt/nginx/html/www/index.html

#重启nginx

[root@web03 opt]# nginx -s reload

#配置客户端机器的hosts文件
在这里插入图片描述

192.168.178.126 www.chaoge.com
192.168.178.126 www.chaoge.com/static/
192.168.178.126 www.chaoge.com/upload/

9.在客户端浏览器查看
www.chaoge.com/static/
在这里插入图片描述

www.chaoge.com/upload/
在这里插入图片描述

www.chaoge.com

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值