Server redirect to the mobile version of the site

    image
    I bring to your attention a simple and cheap (in terms of resources) solution for redirecting mobile device users to a light version of the site. The solution is focused on highload sites, the optimization of which is based on caching guest requests.
    Checking if the client is a mobile device is done by the nginx web server and, if successful, the client is redirected to a subdomain or location. This significantly saves resources and allows for greater scalability compared to PHP methods.

    Configuration for NGINX

    Option number 1. The mobile version is located on the subdomain Option No. 2. Mobile version opened on the same domain
    server{
    < … >
    if ( $http_user_agent ~* (windows\smobile|windows\sce|iphone|ipod|midp|symbian|series\s60|s60|nokia|аndroid| blackberry) ){
    rewrite ^/(.*) m.site.ru$1 permanent;
    }

    location / {
    < … >
    }

    }



    if ( $http_user_agent ~* (windows\smobile|windows\sce|iphone|ipod|midp|symbian|series\s60|s60|nokia|аndroid| blackberry) ){
    rewrite ^/(.*)$ /liteversion/$1 last;
    }


    Identification of a mobile device by $ http_user_agent makes it possible to distinguish mobile clients from ordinary PCs with high accuracy and with minimal resources.
    This is how Google and Yandex work. You can change the user_agent of your browser and see for yourself.

    Of course, I don’t know how exactly the algorithm for determining the "mobile phone" works in G and Y. But I think that it is very similar to the elementary regular expression written above.

    The regular expression contains a list of variations of keywords in the string http_user_agent compiled based on the analysis of the logs of several sites. In total, about 200,000 requests were analyzed per day, 5% of which were sent from mobile devices. This guarantees a high probability that all possible user_agent mobile devices will be listed. I excluded the iPad due to a screen resolution of 1024x768px.

    I hope my note will be useful to you.

    Advanced

    User Agent switch for Mozzyla
    Advanced list of user agents

    Also popular now: