nginx-动静分离

根据扩展名

i
upstream dynamic {
        server 192.9.191.31:8080;
        }       
upstream static {
        server 192.9.191.31:8003;
        }       
server {
        listen 80;
        server_name www.test.com;
        location ~ .*\.(jpg|bmp|png|gif)$ {
                proxy_pass http://static;
                }       
        location ~ .*\.jsp$ {
                proxy_pass http://dynamic;
                }       
    }

基于浏览器和手机类型调度

http {
upstream firefox {
    server 192.9.191.31:8081;
    }
upstream chrome {
    server 192.9.191.31:8082;
    }
upstream iphone {
    server 192.9.191.31:8083;
    }
upstream android {
    server 192.9.191.31:8084;
    }
upstream other { 
    server 192.9.191.31:8085;
    }
server {
    listen 80;
    server_name  192.9.191.30;
    location / {
        if ($http_user_agent ~* "Firefox"){             #匹配firefox浏览器
        proxy_pass http://firefox;
            }       
        if ($http_user_agent ~* "Chrome"){              #匹配chrome谷歌浏览器
        proxy_pass http://chrome;
            }       
        if ($http_user_agent ~* "iphone"){              #匹配iphone手机
        proxy_pass http://iphone;
            }       
        if ($http_user_agent ~* "android"){             #匹配安卓手机
        proxy_pass http://android;
            }       
        proxy_pass http://other;                        #其它浏览器默认访问规则
        }       
    }
}

基于目录

#方式一
upstream static {
        server 192.9.191.31:8081;
        }
upstream upload {
        server 192.9.191.31:8082;
        }
upstream default {
        server 192.9.191.31:8083;
        }
server {
        listen 80;      
        server_name 192.9.191.30;
        location / {    
                proxy_pass http://default; 
                }               
        location /static/ {
                proxy_pass http://static;
                }               
        location /upload/ {
                proxy_pass http://upload;
                }               
        }


#方式二
upstream static {
        server 192.9.191.31:8081;
        }
upstream upload {
        server 192.9.191.31:8082;
        }
upstream default {
        server 192.9.191.31:8083;
        }
server {
        listen 80;      
        server_name 192.9.191.30;
        location / {    
                if ($request_uri ~* "^/static/(.*)$") {
                        proxy_pass http://static;
                        }               

                if ($request_uri ~* "^/upload/(.*)$") {
                        proxy_pass http://upload;
                        }               
                proxy_pass http://default; 
                }  

猜你喜欢

转载自blog.csdn.net/weixin_43342753/article/details/89644717