I am trying to setup my nginx to redirect all the requests from mobile devices to /mobile/$uri i came up with this solution but it doesn\'t seem to work. Is it a syntax problem
#initliaze mobile
set $mobile "";
if ($request_uri !~* "^/mobile.*" ){
set $mobile Y;
}
if ($http_user_agent ~* (iPhone|iPod|android|blackberry) ) {
set $mobile "${mobile}E";
}
if ( $http_referer !~* "xxx\.org" ){
set $mobile "${mobile}S";
}
if ( $host ~* "xxx\.org" ){
set $mobile "${mobile}S";
}
if ($mobile = YESS){
rewrite ^ $scheme://$host/mobile$request_uri ;
}
You should use different locations:
location / {
if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {
return 301 /mobile$request_uri;
}
}
location /mobile/ {
}
btw, http://nginx.org/r/return
Use http://detectmobilebrowsers.com/ It has scripts for all webservers and client/server side languages.