7、Nginx负载均衡深入实践

1、域名不变,基于路径实现动静分离

实现动静分离

http://www.etiantian.org/static/
http://www.etiantian.org/upload/
locatiaon跳转

#lb01与win10的hosts文件加上:
10.0.0.5 www.etiantian.org  bbs.etiantian.org blog.etiantian.org etiantian.org status.etiantian.org
[root@lb01 /application/nginx/conf]#vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream static_pools {
     server 10.0.0.7:80  weight=1;
    }

upstream upload_pools {
         server 10.0.0.8:80  weight=1;
    }

upstream default_pools {
         server 10.0.0.9:80  weight=1;
   }

    server {
        listen       80;
        server_name  www.etiantian.org;

location /static/ { 
	proxy_pass http://static_pools;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload/ { 
	proxy_pass http://upload_pools;
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For $remote_addr;
}
location / { 
	proxy_pass http://default_pools;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

web01(10.0.0.7、模拟静态服务器)

[root@web01 /application/nginx/html/www]#mkdir static
[root@web01 /application/nginx/html/www]#echo static >static/index.html

web02(10.0.0.8、动态)、web03(默认,这里我们在02上做临时ip模拟就行)

[root@web02 /application/nginx/html/www]#mkdir upload
[root@web02 /application/nginx/html/www]#echo upload >upload/index.html

#创建临时IP模拟默认服务器web03
[root@web02 /application/nginx/html/www]#ifconfig eth0:9 10.0.0.9/24 up
[root@web02 /application/nginx/html/www]#ifconfig
[root@web02 /application/nginx/conf/extra]#vim 01_www.conf 
   server {
        listen       80;
        server_name  etiantian.org;
        rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
   }
   server {
        listen      10.0.0.8:80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        }
   server {
        listen      10.0.0.9:80;
        server_name  www.etiantian.org;
        location / {
            root   html/www9;
            index  index.html index.htm;

        }
        autoindex on;
        #auth_basic "oldboy training";
        #auth_basic_user_file /application/nginx/conf/htpasswd;
        access_log logs/access_www.log main;
"01_www.conf" 26L, 678C 已写入                                              
[root@web02 /application/nginx/conf/extra]#mkdir ../../html/www9
[root@web02 /application/nginx/conf/extra]#cd ../../html/www9
[root@web02 /application/nginx/html/www9]#echo www9 >index.html
[root@web02 /application/nginx/html/www9]#echo www9 >oldboy.html
[root@web02 /application/nginx/html/www9]#mkdir new
[root@web02 /application/nginx/html/www9]#echo 9 >new/index.html

浏览器测试:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
如果这里出现502(10.0.0.9没了),在此执行:
[root@web02 /application/nginx/html/www]#ifconfig eth0:9 10.0.0.9/24 up
在这里插入图片描述

2、基于扩展名实现刚才的案例

实现动静分离

location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
	proxy_pass http://static_pools;
}
location / { 
	proxy_pass http://default_pools;
}

location /upload/ { 
	proxy_pass http://upload_pools;
}

3、基于不同类型的设备实现转发

实现PC,手机,不同手机分离

[root@lb01 /application/nginx/conf]#cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

upstream static_pools {
     server 10.0.0.7:80  weight=1;
    }

upstream upload_pools {
         server 10.0.0.8:80  weight=1;
    }

upstream default_pools {
         server 10.0.0.9:80  weight=1;
   }
    server{
        listen     80;
        server_name blog.etiantian.org;
        location / {
        if ($http_user_agent ~* "android")
          {
            proxy_pass http://static_pools;
          }
        if ($http_user_agent ~* "iphone")
          {
            proxy_pass http://upload_pools;
            }
        proxy_pass http://default_pools;
       }
   }
}

[root@lb01 /application/nginx/conf]#curl -A "iphone" http://www.etiantian.org/upload/  
upload

[root@lb01 /application/nginx/conf]#curl -A "iphone" http://www.etiantian.org/static/  
#如果访问static,服务器看你的设备是iphone,调度到upload_pools(10.0.0.8),upload_pools没有static目录,返回404错误结果
[root@lb01 /application/nginx/conf]#curl -A "android" http://www.etiantian.org
www7
#正常访问,ifconfig eth0:9 10.0.0.9/24 up
[root@lb01 /application/nginx/conf]#curl  http://www.etiantian.org
www9

无线局域网访问:
在这里插入图片描述
F12模拟iPhone 手机访问:
在这里插入图片描述

===========================================================

要想浏览器输入blog.etiantian.org转到博客wordpress,只要把lb01的nginx.conf文件改了就行了,如:

[root@lb01 /application/nginx/conf]#cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {
    	server 10.0.0.7:80  weight=1;
    	server 10.0.0.8:80  weight=1;
    }

    server {
   	 listen       80;
    	 server_name  www.etiantian.org;
         location / {
         	proxy_pass http://backend;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_connect_timeout 60; 
		proxy_send_timeout 60;
		proxy_read_timeout 60; 
		proxy_buffer_size 4k; 
		proxy_buffers 4 32k; 
		proxy_busy_buffers_size 64k; 
		proxy_temp_file_write_size 64k;
         }
     }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tony带水!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值