Plan to use Nginx for caching to relieve backend pressure. The website has two versions: mobile phone and desktop. Currently, PHP is used for simple judgment. The code is as follows:
<code>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; }</code>
The idea of judgment in PHP is as follows:
Determine whether the UA has the word Android|iPhone|IEMoble|Mobile
, if it does, it is a mobile phone
Then determine whether there is the word iPad|Tablet
in UA, if it is, it is not a mobile phone
If there is a cookie named ismobile
, regardless of whether the UA is forced to be a mobile phone
What I hope to achieve is to implement the above judgment logic directly on Nginx and set it as cache key (of course it can be done at the same time It is better to send additional headers to the backend, so as to ensure that the judgment results of the front and back ends are absolutely consistent, haha)
Because my English is very poor and I don’t know much about Nginx syntax, etc., I would like to ask an expert if you can give me a DEMO. Thank you!
Plan to use Nginx for caching to relieve backend pressure. The website has two versions: mobile phone and desktop. Currently, PHP is used for simple judgment. The code is as follows:
<code>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; }</code>
The idea of judgment in PHP is as follows:
Determine whether the UA has the word Android|iPhone|IEMoble|Mobile
, if it does, it is a mobile phone
Then judge whether there is the word iPad|Tablet
in UA, if it is, it is not a mobile phone
If there is a cookie named ismobile
, regardless of whether the UA is forced to be a mobile phone
What I hope to achieve is to implement the above judgment logic directly on Nginx and set it as cache key (of course it can be done at the same time It is better to send additional headers to the backend, so as to ensure that the judgment results of the front and back ends are absolutely consistent, haha)
Because my English is very poor and I don’t know much about Nginx syntax, etc., I would like to ask an expert if you can give me a DEMO. Thank you!
Poor English is not the reason. I basically failed in English. Shouldn’t it be the same as Google? At worst, I'll just use a translator.
Make good use of search, and many problems can be solved.
This is the first article I found by searching Google for "nginx determines mobile phone": Portal