欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

7、Nginx负载均衡深入实践

程序员文章站 2022-07-13 08:43:48
...

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
[aaa@qq.com /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、模拟静态服务器)

[aaa@qq.com /application/nginx/html/www]#mkdir static
[aaa@qq.com /application/nginx/html/www]#echo static >static/index.html

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

[aaa@qq.com /application/nginx/html/www]#mkdir upload
[aaa@qq.com /application/nginx/html/www]#echo upload >upload/index.html

#创建临时IP模拟默认服务器web03
[aaa@qq.com /application/nginx/html/www]#ifconfig eth0:9 10.0.0.9/24 up
[aaa@qq.com /application/nginx/html/www]#ifconfig
[aaa@qq.com /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 已写入                                              
[aaa@qq.com /application/nginx/conf/extra]#mkdir ../../html/www9
[aaa@qq.com /application/nginx/conf/extra]#cd ../../html/www9
[aaa@qq.com /application/nginx/html/www9]#echo www9 >index.html
[aaa@qq.com /application/nginx/html/www9]#echo www9 >oldboy.html
[aaa@qq.com /application/nginx/html/www9]#mkdir new
[aaa@qq.com /application/nginx/html/www9]#echo 9 >new/index.html

浏览器测试:
7、Nginx负载均衡深入实践
7、Nginx负载均衡深入实践
7、Nginx负载均衡深入实践
如果这里出现502(10.0.0.9没了),在此执行:
[aaa@qq.com /application/nginx/html/www]#ifconfig eth0:9 10.0.0.9/24 up
7、Nginx负载均衡深入实践

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,手机,不同手机分离

[aaa@qq.com /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;
       }
   }
}

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

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

无线局域网访问:
7、Nginx负载均衡深入实践
F12模拟iPhone 手机访问:
7、Nginx负载均衡深入实践

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

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

[aaa@qq.com /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;
         }
     }
}

相关标签: nginx