服务器准备
主机 | IP | 资源 | 端口 |
---|---|---|---|
lb01 | 10.0.0.4 | 负载均衡 | 80 |
web01 | 172.16.1.7 | Android的页面 | 8081 |
web01 | 172.16.1.7 | iphone的页面 | 8082 |
web01 | 172.16.1.7 | pc的页面 | 8083 |
配置web服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
[root@web01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf [root@web01 ~]# cat /etc/nginx/conf.d/linux.ziyuan.com.conf server { listen 8081; server_name linux.ziyuan.com; location / { root /code/android; index index.html; } } server { listen 8082; server_name linux.ziyuan.com; location / { root /code/iphone; index index.html; } } server { listen 8083; server_name linux.ziyuan.com; location / { root /code/pc; index index.html; } } [root@web01 ~]# systemctl restart nginx |
配置站点目录
1 2 3 4 5 |
[root@web01 ~]# mkdir /code/{android,iphone,pc} [root@web01 ~]# echo "我是pc" > /code/pc/index.html [root@web01 ~]# echo "我是iphone" > /code/iphone/index.html [root@web01 ~]# echo "我是android" > /code/android/index.html [root@web01 ~]# chown -R nginx.nginx /code/ |
配置负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf upstream Android { server 172.16.1.7:8081; server 172.16.1.8:8081; } server { listen 80; server_name linux.ziyuan.com; location / { if ($http_user_agent ~* "Windows") { proxy_pass http://172.16.1.7:8083; } if ($http_user_agent ~* "iPhone") { proxy_pass http://172.16.1.7:8082; } if ($http_user_agent ~* "Android") { proxy_pass http://Android; } return 500; } } |