Sometimes because of the demand, we need to distinguish between the PC side and the mobile side of the visitor.
Here are a few simple ways to introduce:
Nginx Block mobile access:
1 2 3 4 5 6 7 8 9 10 |
server { .... if ($http_user_agent !~* '(Android|webOS|iPhone|iPad|BlackBerry)') { return 444; } ... } |
otherwise Nginx Block PC access:
1 2 3 4 5 6 7 8 9 10 |
server { .... if ($http_user_agent ~* '(Android|webOS|iPhone|iPad|BlackBerry)') { return 444; } ... } |
I met a friend today and need to block PC visitors, but he need to specify a directory to not block PC visitors.
After some testing, the following methods are feasible:
Do not block the test_admin directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server { .... set $mobile ''; if ($http_user_agent !~* '(Android|webOS|iPhone|iPad|BlackBerry)') { set $mobile "1"; } if ($request_uri !~* "test_admin") { set $mobile "${mobile }2"; } if ( $mobile = 12 ){ return 444; } ... } |
Simple record, in case of need.
This article was first published by V on 2018-10-18 and can be reprinted with permission, but please be sure to indicate the original link address of the article :http://www.nginxer.com/records/nginx-blocks-mobilepc-access/