Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 notnotmobile.appspot.com.
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;
}
@perusio

This comment has been minimized.

Owner

perusio commented Dec 1, 2011

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

@perusio

This comment has been minimized.

Owner

perusio commented Jan 18, 2012

Added the bot detection.

@passani

This comment has been minimized.

passani commented Nov 2, 2012

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.

http://www.scientiamobile.com/blog/post/view/id/25/title/HTTP-and-Mobile%3A-The-Missing-Header-

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

Thanks

Luca

@umgeher

This comment has been minimized.

umgeher commented Jan 31, 2014

Thanks!

@hackingman

This comment has been minimized.

hackingman commented Nov 1, 2014

Would you please give an usage example?

@yavuzyurtbegendi

This comment has been minimized.

yavuzyurtbegendi commented Oct 20, 2015

Yes, usage example would be really helpfull

@bertalanimre

This comment has been minimized.

bertalanimre commented Apr 18, 2016

I'm intrested in an usage example too!

@soledadmedia

This comment has been minimized.

soledadmedia commented Jun 8, 2016

Here's how I use it (to redirect mobile traffic to an m.domain.com site):

set $mobile_rewrite do_not_perform;

if ($is_mobile) {
    set $mobile_rewrite perform;
}

if ($mobile_rewrite = perform) {
    rewrite ^ http://m.domain.com$request_uri? redirect;
    break;
}

Would be awesome though if there was a way to separate mobile from tablet from desktop.

@marianocibelli

This comment has been minimized.

marianocibelli commented Feb 23, 2017

Its a pretty old post but in case someone want to detect tablet and mobile separately here is my code

map $http_user_agent $ua_device {
	default 'desktop';
	~*(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|mobile.+firefox|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ ce|xda|xiino/i 'mobile';
	~*android|ipad|playbook|silk/i 'tablet';
}

add_header x-ua-device $ua_device;

@bsushant-athena

This comment has been minimized.

bsushant-athena commented Jun 1, 2017

How to block requests from rest clients , I tried this but still I'm not able to block Postman requests ?
map $http_user_agent $is_browser {
~*(MSIE|Firefox|Safari|Opera) 0;
~*spider|crawl|slurp|bot 1; # bots
default 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment