Skip to content

Instantly share code, notes, and snippets.

@ArrayIterator
Last active November 3, 2023 01:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ArrayIterator/a4a294b121cb8323c79748cba9968cdc to your computer and use it in GitHub Desktop.
Save ArrayIterator/a4a294b121cb8323c79748cba9968cdc to your computer and use it in GitHub Desktop.
Nginx Configuration Detect User-Agent
server {
listen 80;
listen 443 ssl http2;
server_name example.com;
root /path/to/public/www;
# ADD HEADERS
# Using nginx-http-mod-headers-more
# https://github.com/openresty/headers-more-nginx-module
more_set_headers 'X-Browser-Os: $client_browser_os';
more_set_headers 'X-Browser-Name: $client_browser_name';
more_set_headers 'X-Browser-Version: $client_browser_version';
more_set_headers 'X-Browser-Device: $client_browser_device';
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
# CUSTOM MAP DETECT USER AGENT
# INCLUDE on nginx.conf
map $http_user_agent $client_browser_operating_system {
default '';
'~^\s*Mozilla/[^\(]+\s*\(\s*([^\)]+[\)]?\s*)\)' $1;
}
map $http_user_agent $client_browser_identifier {
default '';
'~^\s*Mozilla/.+\)\s+([^/]+/[^\s]+.*)\s*' $1;
}
map $client_browser_operating_system $client_browser_os {
default '';
'~*(ip[ao]d|iphone|ios)' iOS;
'~*(symbian)' Symbian;
'~*(android)' Android;
'~*Win(dows)?[^;0-9]*11' 'Windows 11';
'~*Win(dows)?[^;0-9]*10' 'Windows 10';
'~*Win(dows)?[^;0-9]*8' 'Windows 8';
'~*Win(dows)?[^;0-9]*7' 'Windows 7';
'~*Win(dows)?[^;0-9]*6' 'Windows XP';
'~*Win' Windows;
'~*fedora' Fedora;
'~*cloudlinux' CloudLinux;
'~*mint' LinuxMint;
'~*Elementary.*Linux' 'Elementary Os';
'~*ubuntu' Ubuntu;
'~*centos' CentOS;
'~*redhat' Redhat;
'~*arch' ArchLinux;
# MAC OS - ADD ADDITIONAL VERSION by 10.xx or 11.xx or 12.xx
'~*Mac[^0-9]+12[\._][0-9]+' 'MacOS Monterey';
'~*Mac[^0-9]+(?:10[\._]16|11[\._][0-9]+)' 'MacOS BigSur';
'~*Mac[^0-9]+10[\._]15' 'MacOS Catalina';
'~*Mac[^0-9]+10[\._]14' 'MacOS Mojave';
'~*Mac[^0-9]+10[\._]13' 'MacOS High Sierra';
'~*Mac[^0-9]+10[\._]12' 'MacOS Sierra';
'~*Mac[^0-9]+10[\._]11' 'MacOS X El-Capitan';
'~*Mac[^0-9]+10[\._]10' 'MacOS X Yosemite';
'~*Mac[^0-9]+10[\._]9' 'MacOS X Mavericks';
'~*Mac[^0-9]+10[\._]8' 'MacOS X Mountain Lion';
'~*Mac[^0-9]+10[\._]7' 'MacOS X Lion';
'~*Mac[^0-9]+10[\._]6' 'MacOS X Snow Leopard';
'~*Mac[^0-9]+10[\._]5' 'MacOS X Leopard';
'~*Mac[^0-9]+10[\._]4' 'MacOS X Tiger';
'~*Mac[^0-9]+10[\._]3' 'MacOS X Panther';
'~*Mac[^0-9]+10[\._]2' 'MacOS X Jaguar';
'~*Mac[^0-9]+10[\._]1' 'MacOS X Puma';
'~*Mac' MacOS;
'~*linux' Linux;
}
map $client_browser_operating_system $client_browser_device {
default '';
'~*iPad' iPad;
'~*iPhone' iPhone;
#! SAMSUNG - JUST FOR GUESS
# ----------------------------
# Galaxy S
'~*[^;]+;[^;]+;\s*GT-I9300' 'Samsung Galaxy S3';
'~*[^;]+;[^;]+;\s*SCH-I545' 'Samsung Galaxy S4';
'~*[^;]+;[^;]+;\s*SM-G900' 'Samsung Galaxy S5';
'~*[^;]+;[^;]+;\s*SM-G920' 'Samsung Galaxy S6';
'~*[^;]+;[^;]+;\s*SM-G925' 'Samsung Galaxy S6 Edge';
'~*[^;]+;[^;]+;\s*SM-G930' 'Samsung Galaxy S7';
'~*[^;]+;[^;]+;\s*SM-G935' 'Samsung Galaxy S7 Edge';
'~*[^;]+;[^;]+;\s*SM-G950' 'Samsung Galaxy S8';
'~*[^;]+;[^;]+;\s*SM-G955' 'Samsung Galaxy S8+';
'~*[^;]+;[^;]+;\s*SM-G960' 'Samsung Galaxy S9';
'~*[^;]+;[^;]+;\s*SM-G965' 'Samsung Galaxy S9+';
'~*[^;]+;[^;]+;\s*SM-G973' 'Samsung Galaxy S10';
# Galaxy A
'~*[^;]+;[^;]+;\s*SM-(A[3-9])' 'Samsung Galaxy $1';
# NOTE
'~*[^;]+;[^;]+;\s*SM-N90' 'Samsung Galaxy Note 3';
'~*[^;]+;[^;]+;\s*SM-N91' 'Samsung Galaxy Note 4';
'~*[^;]+;[^;]+;\s*SM-N92' 'Samsung Galaxy Note 5';
'~*[^;]+;[^;]+;\s*SM-N93' 'Samsung Galaxy Note 6';
'~*[^;]+;[^;]+;\s*SM-N94' 'Samsung Galaxy Note 7';
'~*[^;]+;[^;]+;\s*SM-N95' 'Samsung Galaxy Note 8';
'~*[^;]+;[^;]+;\s*SM-N96' 'Samsung Galaxy Note 8';
'~*[^;]+;[^;]+;\s*S(CH|M)-G' 'Samsung Galaxy';
'~*[^;]+;[^;]+;\s*SM-N' 'Samsung Galaxy Note';
'~*[^;]+;[^;]+;\s*GT' 'Samsung Galaxy Tab';
# ADD OTHER HERE
# ----
# Just Detect End Of Data
'~*[^;]+;[^;]+;\s*(.+)\s*(?:Build/.*)$' $1;
'~*[^;]+;[^;]+;\s*(.+)\s*(?:\s+[^/]+/.*)$' $1;
'~*[^;]+;[^;]+;\s*(.+)\s*$' $1;
'~*[^;]+;[^;]+;\s*([^/]+)$' $1;
'~*([^;]+);\s*[^;/]+$' $1;
}
map $client_browser_identifier $client_browser_name {
default '';
'~*Chrome/.*Mobile' 'Chrome Mobile';
'~*Chrome/' 'Chrome';
'~*Firefox/.*Mobile' 'Firefox Mobile';
'~*Firefox/' 'Firefox';
'~*Opera/.*Mobile' 'Opera Mobile';
'~*Opera/' 'Opera';
'~*Opera Mini' 'Opera Mini Mobile';
'~*^(?:Version/[^/\s]+)?\s*(?:Mobile/[^/\s]+)([^/]+)/' '$1 Mobile';
'~*([^/]+)/.+Mobile' '$1 Mobile';
'~*([^/]+)/' $1;
}
map $client_browser_identifier $client_browser_version {
default '';
'~[^/]+/([^\s]+)' $1;
}
@alyyousuf7
Copy link

alyyousuf7 commented May 21, 2022

When User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1, it returns Safari Mobile with a leading space (GitHub's markdown doesn't show leading space).

This need to change:

'~*^(?:Version/[^/\s]+)?\s*(?:Mobile/[^/\s]+)([^/]+)/' '$1 Mobile';

to

'~*^(?:Version/[^/\s]+)?\s*(?:Mobile/[^/\s]+)\s*([^/]+)/' '$1 Mobile';

@ArrayIterator
Copy link
Author

When User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1, it returns Safari Mobile with a leading space (GitHub's markdown doesn't show leading space).

This need to change:

'~*^(?:Version/[^/\s]+)?\s*(?:Mobile/[^/\s]+)([^/]+)/' '$1 Mobile';

to

'~*^(?:Version/[^/\s]+)?\s*(?:Mobile/[^/\s]+)\s*([^/]+)/' '$1 Mobile';

it's ok cause [^/]+ it also allow contains whitespace.

BTW, this gist just for fun 😅

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