nginx动静分离

1.最好的情况是在业务上做动静分离,如果业务没有做此功能,nginx也可以实现

2.方式1:利用URL匹配

 

访问地址:http://www.vijay.com/status

3.方式2:利用扩展名匹配

3.方式3:利用rewrite中的if(此方式还可以用于判断浏览器,如判断浏览器是IE6,则转发提示页面,提示浏览器版本低,请升级)

4.根据user客户端类型(win,mac,ios,安卓)进行请求分配:(关键参数:proxy_set_header http_user_agent $http_user_agent;)

 

        #样式
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|font|ttf)$ {
                if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)){
                        root /usr/local/nginx/html/proxy_mobile;
                }
                root /usr/local/nginx/html/dist;
        }

        #后台数据接口
        location ~* /(ipagent)/ {
                proxy_pass http://backend;
        }


        #首页
        location  / {
            #root   /usr/local/nginx/html/;
            #index  index.html index.htm index.html;
            if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)){

                        #try_files $uri $uri/proxy_mobile/ /index.html;
                        #index  index.html index.htm;
                        root /usr/local/nginx/html/proxy_mobile;
                        #index  index.html index.htm;
                }


            try_files $uri $uri/dist /index.html;
       }

 

posted on 2016-05-12 16:56  vijayfly  阅读(254)  评论(0编辑  收藏  举报

导航