nginx资源分离实践
1.准备环境
主机 | IP | 功能 |
---|---|---|
lb01 | 10.0.0.4,172.16.1.4 | 负载均衡 |
web01 | 172.16.1.7 | Android页面 |
web02 | 172.16.1.8 | iPhone页面 |
web03 | 172.16.1.9 | PC端页面 |
2.配置web01服务器
1)配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf server { listen 80; server_name linux.sj.com; charset utf8; location / { root /code/android; index index.html; } } [root@web01 ~]# systemctl restart nginx
2)创建站点目录
[root@web01 ~]# mkdir /code/android [root@web01 ~]# echo "我是android" >> /code/android/index.html [root@web01 ~]# chown -R www.www /code/android/
3)访问测试
1.配置hosts 10.0.0.7 linux.sj.com
3.配置web02服务器
1)配置nginx
[root@web02 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf server { listen 80; server_name linux.sj.com; charset utf8; location / { root /code/iphone; index index.html; } }
2)创建站点文件
[root@web02 ~]# mkdir /code/iphone [root@web02 ~]# echo "我是Iphone" >> /code/iphone/index.html [root@web02 ~]# chown -R www.www /code/iphone/
3)访问测试
1.配置hosts 10.0.0.8 linux.sj.com
4.配置web03服务器
1)配置nginx
[root@web03 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf server { listen 80; server_name linux.sj.com; charset utf8; location / { root /code/pc; index index.html; } } [root@web02 ~]# systemctl restart nginx
2)创建站点文件
[root@web03 ~]# mkdir /code/pc -p [root@web03 ~]# echo "我是pc端" >> /code/pc/index.html [root@web03 ~]# chown -R www.www /code/
3)访问测试
1.配置hosts 10.0.0.9 linux.sj.com
5.配置负载均衡
1.配置nginx
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf upstream android { server 10.0.0.7; } upstream iphone { server 10.0.0.8; } upstream pc { server 10.0.0.9; } server { listen 80; server_name linux.sj.com; location / { if ($http_user_agent ~* "Android") { #判断如果是安卓端 proxy_pass http://android; #代理到android虚拟主机池 } if ($http_user_agent ~* "iPhone") { #判断如果是苹果端 proxy_pass http://iphone; #代理到iphone虚拟主机池 } if ($http_user_agent ~* "WOW64") { #判断如果是IE浏览器 return 403; #直接返回403 } proxy_pass http://pc; #如果没有匹配到以上内容,默认都代理到pc虚拟主机池 include proxy_params; } }
2.重启访问
1.重启 [root@lb01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@lb01 ~]# systemctl restart nginx 2.配置hosts 10.0.0.4 linux.sj.com 3.访问