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:
Determine if UA has any Android|iPhone|IEMoble|Mobile
words, or mobile phones
And then judge whether there are any words in UA iPad|Tablet
, or not mobile phones.
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:
Determine if UA has any Android|iPhone|IEMoble|Mobile
words, or mobile phones
And then judge whether there are any words in UA iPad|Tablet
, or not mobile phones.
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