nginx负载均衡企业中的应用(linux运维24)

1. 实现动静太页面的分离

/upload 集群-192.168.246.8:80 html/www/upload upload服务器集群
/static 集群-192.168.246.7:80 html/www/static static服务器集群
/ 集群-192.168.246.9:80 html/www default服务器集群

1.1 各个web集群的nginx配置文件

web01 web02 web03

vim /etc/nginx/conf.d/www.conf
server{
  listen 80;
  server_name www.yq.com;
  location / {
       root /html/www;
       index yq.html;
   }

}

web01 192.168.246.7

mkdir /html/www/static
echo "static web01 7" > /html/www/static/yq.html
echo "web01 7" > html/www/yq.html
chown -R www.www /html

web 02 192.168.246.8

mkdir /html/www/upload
echo "upload web02 8" > /html/www/static/yq.html
echo "web02 8" > html/www/yq.html
chown -R www.www /html

web03 192.168.246.9

mkdir /html/www/
echo "defaultweb03 9" > /html/www/yq.html
chown -R www.www /html

web 01 02 03

systemctl reload nginx

1.2 负载均衡服务器的nginx配置文件

vim /etc/nginx/conf.d/lb.conf
upstream static  {
       server 192.168.246.7:80;
    }
upstream upload  {
       server 192.168.246.8:80;
    }
upstream default  {
       server 192.168.246.9:80;
    }
server {
         
        listen       80;
        server_name  www.yq.com;
        location / {
           proxy_pass http://default;
           proxy_set_header Host $host;
           proxy_next_upstream error timeout http_404 http_502 http_403;
	   proxy_set_header X-Forwarded-For $remote_addr;
        }
       
        location /upload {
           proxy_pass http://upload;
           proxy_set_header Host $host;
           proxy_next_upstream error timeout http_404 http_502 http_403;
	   proxy_set_header X-Forwarded-For $remote_addr;
        }
        location /static {
           proxy_pass http://static;
           proxy_set_header Host $host;
           proxy_next_upstream error timeout http_404 http_502 http_403;
	   proxy_set_header X-Forwarded-For $remote_addr;
        }
    }

systemctl reload nginx

1.3 动静分离测试

修改windows的hosts

192.168.246.5 www.yq.com

浏览器输入:

www.yq.com
www.yq.com/upload
www.yq.com/static

负载均衡服务器访问:

curl -H host:www.yq.com  192.168.246.5
curl -H host:www.yq.com  192.168.246.5/upload/
curl -H host:www.yq.com  192.168.246.5/static/

2. 根据不同的访问终端显示不同的页面(手机,浏览器)

  if ($http_user_agent ~* iphone){
                  proxy_pass http://mobile;
             } 

2.1 环境的准备

iphone www.oldboy.com — iphone_access 192.168.246.7:80 mobile移动端集群
谷歌 www.oldboy.com — google_access 192.168.246.8:80 web端集群
IE 360 www.oldboy.com — default_access 192.168.246.9:80 default端集群

web01:
echo "iphone_access 192.168.246.7" >/html/www/yq.html

web02:
echo "google_access 192.168.246.8" >/html/www/yq.html

web03:
echo "default_access 192.168.246.9" >/html/www/yq.html

2.配置文件的编写

负载均衡服务器192.168.246.5

vim /etc/nginx/conf.d/lb.conf
upstream mobile  {
       server 192.168.246.7:80;
    }
upstream web  {
       server 192.168.246.8:80;
    }
upstream default  {
       server 192.168.246.9:80;
    }
server {
         
        listen       80;
        server_name  www.yq.com;
        location / {
           if ($http_user_agent ~* iphone){
                  proxy_pass http://mobile;
             } 
           if ($http_user_agent ~* Chrome){
                  proxy_pass http://web;
             } 
           proxy_pass http://default;
           proxy_set_header Host $host;
           proxy_next_upstream error timeout http_404 http_502 http_403;
	   proxy_set_header X-Forwarded-For $remote_addr;
        }
  }
systemctl reload nginx

浏览器访问即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长安有故里y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值