Nginx 根据访问终端跳转页面


原文链接: Nginx 根据访问终端跳转页面

关闭蜘蛛

server {

    if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
        {
            return 403; 
        }
}

Nginx 根据访问终端跳转页面

server {
 server_name pytool.com;
 listen 80;
 access_log off;
 location / {
    if ($http_user_agent ~ "((MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT)|(SonyEricsson)|(NEC)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC)|(SED)|(EMOL)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera))"){
      rewrite ^/(.*)$ http://221.180.20.228:8081/wap/$1 ;
    }

    if ( $http_user_agent ~ ^$ )  {
      rewrite ^/(.*)$ http://221.180.20.228:8081/wap/$1 ;
    }

 proxy_pass http://221.180.20.228:8080;
 }
}

手机端重定向

location / {
  index  index.htm index.html index.php;
  if (!-e $request_filename){
  #地址作为将参数rewrite到index.php上。tp框架接收s参数为controller和action,不少框架都利用这种方式来实现伪pathinfo模式(pathinfo为php功能,nginx并不支持)
  rewrite ^/(.*)$ /index.php?s=$1 last;
  break;
  }

  # 手机端重写
  if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
    rewrite ^/(.*)$ http://$host/mobile/$1 permanent;    #进行正常访问的时候判断user_agent为手机,则重写至此页面
  }
}

location /mobile {
  index mobile.php

}
`