Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Mobile device detection in Nginx with just 7 lines of configuration
### Testing if the client is a mobile or a desktop.
### The selection is based on the usual UA strings for desktop browsers.
## Testing a user agent using a method that reverts the logic of the
## UA detection. Inspired by
map $http_user_agent $is_desktop {
default 0;
~*linux.*android|windows\s+(?:ce|phone) 0; # exceptions to the rule
~*spider|crawl|slurp|bot 1; # bots
~*windows|linux|os\s+x\s*[\d\._]+|solaris|bsd 1; # OSes
## Revert the logic.
map $is_desktop $is_mobile {
1 0;
0 1;

I've updated the directive to support IE9 on Windows Phone.


Added the bot detection.


The following is a reference to an open (but not open source) solution provided by my company to detect mobile devices (or any kind of HTTP client in fact) through NGINX.

I figured that companies looking for a professionally supported solution in this space may want to know about it.






Would you please give an usage example?


Yes, usage example would be really helpfull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.