mobile version detect with same url in nginx using proxy_pass :-

Friday, March 30, 2018

Mobile version detect with same url in nginx using proxy_pass :-

if ($http_user_agent ~* "/Mobile|Android|BlackBerry/") {
    proxy_pass    http://m.example.com$request_uri;
}

proxy_pass    http://www.example.com$request_uri;


http {
    upstream app-pc {
        server 127.0.0.1:8001;
    }

    upstream app-mobile {
        server 127.0.0.1:8002;
    }

    server {
        listen       8080;
        server_name  localhost;

        # check user agent
        if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)') {
          set $ua_type "@mobile";
        }

        location / {
            # root
            if ($ua_type = "@mobile"){
                set $page_cache_path /var/www/app_pc/public;
            }
            if ($ua_type != "@mobile"){
                set $page_cache_path /var/www/app_mobile/public;
            }
            root $page_cache_path;

            # for page cache
            if (-f $request_filename) { 
                break; 
            }
            if (-f $request_filename/index.html) {
                rewrite (.*) $1/index.html break;
            }
            if (-f $request_filename.html) {
                rewrite (.*) $1.html break;
            }

            # proxy
            if ($ua_type = "@mobile"){
                proxy_pass http://app-mobile;
            }
            if ($ua_type != "@mobile"){
                proxy_pass http://app-pc;
            }
        }
    }
}

0 comments:

About This Blog

Lorem Ipsum

  © Copyright 2009 Linux-HelpLine.Blogspot.com

Back to TOP