📜 ⬆️ ⬇️

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 users of mobile devices 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 whether a client is a mobile device is performed 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. Mobile version is located on the subdomain
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 / {
< … >
}

}


Option number 2. Mobile version opened on the same domain.
if ( $http_user_agent ~* (windows\smobile|windows\sce|iphone|ipod|midp|symbian|series\s60|s60|nokia|ndroid| blackberry) ){
rewrite ^/(.*)$ /liteversion/$1 last;
}

')
Determining a mobile device by $ http_user_agent allows distinguishing 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 make sure of it.

Of course, I do not know how exactly the algorithm for determining the “cell phone” in G and Y works. But I think that it is very similar to the elementary regular expression written above.

The regular expression contains a list of keyword variations in the http_user_agent line, compiled based on the analysis of logs from several sites. In total, about 200,000 requests per day were analyzed, 5% of which were sent from mobile devices. This ensures a high probability of reaching the lists of all possible user_agent mobile devices. I excluded iPad because of the screen resolution of 1024x768px.

I hope my note will be useful to you.

Additionally

Switch User Agent for Mozzily
Extended list of user agents

Source: https://habr.com/ru/post/108242/


All Articles