Created
October 8, 2015 11:00
-
-
Save yoavmatchulsky/7a04a6008ca06a65e9de to your computer and use it in GitHub Desktop.
Mobile detection configuration for nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /var/log/nginx/conf.d/mobile.conf | |
map $http_user_agent $mobile { | |
default 0; | |
~*(android|bb\d+|meego).+mobile|ip(hone|od)|opera\sm(ob|in)i|mobile.+firefox 1; | |
~*iemobile|phone|symbian|windows\sce|blackberry|up\.(browser|link) 1; | |
~*avantgo|bada\/|blazer|compal|elaine|fennec|hiptop|iris|kindle|lge\s|maemo 1; | |
~*midp|mmp|netfront|palm(\sos)?|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0 1; | |
~*treo|vodafone|wap|xda|xiino 1; | |
~*^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\swa|abac|ac(er|oo|s\-)) 1; | |
~*^(ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)) 1; | |
~*^(attw|au(di|\-m|[rs]\s)|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)) 1; | |
~*^(br[ev]w|bumb|bw\-[nu]|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-) 1; | |
~*^(co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do[cp]o) 1; | |
~*^(ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)) 1; | |
~*^(fetc|fly[\-_]|g1\su|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie) 1; | |
~*^(hcit|hd\-[mpt]|hei\-|hi(pt|ta)|hp(\si|ip)|hs\-c|ht(c[\-\s_agpst]|tp)) 1; | |
~*^(hu(aw|tc)|i\-(20|go|ma)|i230|iac[\s\-\/]|ibro|idea|ig01|ikom|im1k) 1; | |
~*^(inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt[\s\/]|klon) 1; | |
~*^(kpt\s|kwc\-|kyo[ck]|le(no|xi)|lg(\sg|\/[klu]|50|54|\-[a-w])|libw) 1; | |
~*^(lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)) 1; | |
~*^(mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t[\-\sov]|zz)|mt(50|p1|v\s)|mwbp) 1; | |
~*^(mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50[025]|n7(0[01]|10)) 1; | |
~*^(ne([cm]\-|on|tf|wf|wg|wt)|nok[6i]|nzph|o2im|op(ti|wv)|oran|owg1|p800) 1; | |
~*^(pan[adt]|pdxg|pg(13|\-[1-8c])|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)) 1; | |
~*^(prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600) 1; | |
~*^(raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|oo|[hp]\-)) 1; | |
~*^(sdk\/|se(c[\-01]|47|mc|nd|ri)|sgh\-|shar|sie[\-m]|sk\-0|sl(45|id)) 1; | |
~*^(sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|[hv]\-|v\s)|sy(01|mb)|t2(18|50)) 1; | |
~*^(t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel[im]|tim\-|t\-mo|to(pl|sh)) 1; | |
~*^(ts(70|m[\-35])|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)) 1; | |
~*^(vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)) 1; | |
~*^(w3c[\-\s]|webc|whit|wi(g\s|nc|nw)|wmlb|wonu|x700|yas\-|your|zeto) 1; | |
~*^zte\- 1; | |
} | |
map $mobile $desktop { | |
default 1; | |
1 0; | |
0 1; | |
} | |
# nginx.conf | |
http { | |
... | |
# split logs for mobile and desktop (desktop is less relevant to me) | |
log_format mobile_log '$remote_addr - $remote_user [$time_local] ' | |
'"$request" $status $body_bytes_sent ' | |
'"$http_referer" "$http_user_agent" ' | |
'$request_time $upstream_response_time $pipe'; | |
log_format desktop_log '$remote_addr [$time_local] ' | |
'[$request] [$http_referer] [$http_user_agent]'; | |
access_log /var/log/nginx/access.mobile.log mobile_log if=$mobile; | |
access_log /var/log/nginx/access.desktop.log desktop_log if=$desktop; | |
# This probably exists, so don't use it twice | |
include /etc/nginx/conf.d/*.conf; | |
} | |
# for example, if I want my desktop users to receive a blank response | |
server { | |
... | |
if ($desktop) { | |
return 200; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment