Nginx How to judge the phone

Source: Internet
Author: User
Plan to use Nginx cache to mitigate back-end pressure. The website has two versions of mobile phone and desktop, currently using PHP for simple judgment, the code is as follows:

function isMobile(){        $ua = $_SERVER['HTTP_USER_AGENT'];        $ismobile = preg_match('/Android|iPhone|IEMoble|Mobile/i', $ua);        $ismobile = preg_match('/iPad|Tablet/i', $ua) ? 0 : $ismobile;        $ismobile = isset($_COOKIE['ismobile']) ? 1 : $ismobile;        return $ismobile;    }

In PHP the idea of judging is this:

    1. Determine if UA has any Android|iPhone|IEMoble|Mobile words, or mobile phones

    2. And then judge whether there are any words in UA iPad|Tablet , or not mobile phones.

    3. If there is ismobile a cookie named, whether the UA is forced to be set to the phone
      What I want to achieve is to implement the above judgment logic directly on Nginx, and set as the cache key (of course, it is better to send additional headers at the same time to the back end, so as to ensure that the front and back end of the result is absolutely consistent, haha)

Because my English is very poor, and nginx grammar and so do not understand, so want to ask the big God, can give a demo, thank you!

Reply content:

Plan to use Nginx cache to mitigate back-end pressure. The website has two versions of mobile phone and desktop, currently using PHP for simple judgment, the code is as follows:

function isMobile(){        $ua = $_SERVER['HTTP_USER_AGENT'];        $ismobile = preg_match('/Android|iPhone|IEMoble|Mobile/i', $ua);        $ismobile = preg_match('/iPad|Tablet/i', $ua) ? 0 : $ismobile;        $ismobile = isset($_COOKIE['ismobile']) ? 1 : $ismobile;        return $ismobile;    }

In PHP the idea of judging is this:

    1. Determine if UA has any Android|iPhone|IEMoble|Mobile words, or mobile phones

    2. And then judge whether there are any words in UA iPad|Tablet , or not mobile phones.

    3. If there is ismobile a cookie named, whether the UA is forced to be set to the phone
      What I want to achieve is to implement the above judgment logic directly on Nginx, and set as the cache key (of course, it is better to send additional headers at the same time to the back end, so as to ensure that the front and back end of the result is absolutely consistent, haha)

Because my English is very poor, and nginx grammar and so do not understand, so want to ask the big God, can give a demo, thank you!

Poor English is not a reason, I basically did not pass the English, still not the same google? Big deal with translation.
A lot of problems can be solved by the use of search.
This is the first article I found using Google search "Nginx judgment Phone": Portal

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.