Detect Mobile User Agent in Nginx

Hafiq Iqmal
Geek Culture
Published in
3 min readApr 19, 2022

--

You might notice that some website have 2 different URL for mobile view and desktop view like Facebook. Facebook has m.facebook.com for mobile view and facebook.com for desktop view. Usually, the main website, they don’t care about responsive design. If customer want to well fit the in mobile view, just use the another website which only for mobile view like m.facebook.com (with subdomain of m indicate mobile).

In my opinion, for some reason why developers or companies separate their website responsiveness because building a responsive website is quite painful where need to maintain various of screens. If we separate it into mobile subdomain, we can just focus only on how in mobile UI looks like.

If you open your browser in your mobile phone, you may notice the “Desktop site” checkbox in the setting.

The checkbox “Desktop site” able to switch your browser user agent between mobile agent and desktop agent. If you open facebook.com, you will automatically redirect to m.facebook.com because the server detect your browser as mobile agent. But if you click “Desktop site”, you will be redirect to facebook.com site. Quite awesome right?

In this article, i will show you how we can make the same redirect as facebook do using Nginx.

Let’s get started

--

--

Hafiq Iqmal
Geek Culture

Technical Lead | Software Engineer | Laravel Enthusiasts | Tech writer | UiTM Alumni | Husband | Proud father of a beautiful daughter