If you’re using NGINX and you want to detect mobile devices, I just finished a loose port of Mobile-Detect to Lua brilliantly named mobile-detect.lua.
The script detects the device by comparing patterns against a given User-Agent string.
The following device information is available:
- mobile or not
- if mobile, whether phone or tablet
- operating system
- Mobile Grade (A, B, C)
- specific versions (e.g. WebKit)
The script is documented and fully unit tested.
Requirements:
- NGINX compiled with Lua module
- OpenResty bundle is recommended
- CJSON
- If using OpenResty, CJSON is preinstalled
- If not: luarocks install lua-cjson
- Lrexlib – PCRE
- luarocks install lrexlib-PCRE
Usage:
1 2 3 4 5 6 7 8 9 10 |
init_by_lua_block { mobile_detect = require "mobile-detect" } content_by_lua_block { local user_agent = ngx.var.http_user_agent ngx.say(mobile_detect.mobile(user_agent)) ngx.say(mobile_detect.is('iPhone', user_agent)) ngx.say(mobile_detect.is('bot', user_agent)) } |
Script:
mobile-detect.lua