基于上次的lvs(DR)+keepalived负载均衡到后端的nginx01和nginx02,将nginx01和nginx02做一个反向代理到后面的nginx静态代码和nginx+php动态代码,实现动静分离。
直接在上次的拓扑图加上两台机器。
lvs01: 192.168.40.10
lvs02: 192.168.40.11
nginx01:192.168.40.12
nginx02:192.168.40.13
静态: 192.168.40.15
动态: 192.168.40.17
vip: 192.168.40.100
1、首先配置nginx01的nginx配置文件,/usr/local/nginx/conf/nginx.conf,添加两个upstream模块,分别是静态的ip和动态的ip:
upstream dynamic_web{
server 192.168.40.17:80 max_fails=3 fail_timeout=20s weight=2;
}
upstream static_web{
server 192.168.40.15:80 max_fails=3 fail_timeout=20s weight=2;
}
2、配置location,利用正则分离动态和静态:
location ~ \.php?$ { ##后缀为.php结尾的
proxy_pass http://dynamic_web; ##反向代理到动态ip
}
location ~ \.(jpg|png|swf|gif)?$ { ##后缀匹配
proxy_pass http://static_web;
}
location ~ / { ##访问根目录
proxy_pass http://static_web;
}
将nginx的配置文件scp到nginx02:
[root@nginx01~]#scp/usr/local/nginx/conf/nginx.conf192.168.40.13:/usr/local/nginx/conf/nginx.conf
检查配置文件是否有问题,没问题就直接启动nginx:
[root@nginx01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx01 ~]# /usr/local/nginx/sbin/nginx -s reload
4、配置后端的静态nginx服务器,直接安装好nginx后修改主目录到/web,然后上传一张图片和写入一个html文件做测试:
[root@static01 web]# ls
1.jpg index.html
[root@static01 web]# cat index.html
static
将nginx启动。
5、配置后端动态服务器,环境是nginx+php,首先配置nginx支持php,然后写入一个php代码网页做测试:
[root@dynamic01 web]# ls
index.php
[root@dynamic01 web]# cat index.php
<?php
echo "Hello World\n"
?>
然后将nginx启动,在将php启动,就可以直接测试了。
6、结合上次的lvs(DR)+keepalived环境,直接在本地浏览器访问vip测试nginx反向代理服务器的正则匹配是否成功。
首先匹配的是图片和静态网页:
再测试动态php代码:
这样就实现动静分离了。
转载于:https://blog.51cto.com/lsfandlinux/2044396