nginx实现pc端和电脑端的自动跳转!

设备 官网网址
pc端

www.peng.com

移动端 m.peng.con

需求背景:在pc访问二个官网时只可以跳转www.peng.com,在移动端访问时只可以跳转到m.one.com。

pc端nginx配置如下:

server {
      listen       80;
      server_name  www.peng.com;

      #charset koi8-r;
      #access_log  logs/host.access.log  main;
    # 下面根据user_agent可以获取
     if ($http_host !~ "^www.one.cn$") {
      rewrite  ^(.*)    http://www.peng.cn$1 permanent;
     }
     if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
      rewrite  ^(.*)    http://m.peng.com$1 permanent;
     }
    location / {
            root     /home/build/rampage-home-front/dist/html;
            index  index.html index.htm;
     }

}


移动端:

server {
      listen       80;
      server_name  m.one.cn;

      #charset koi8-r;
      #access_log  logs/host.access.log  main;
    #非移动端跳转到 www.peng.com
     if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
      rewrite  ^(.*)    http://www.peng.com$1 permanent;
     }

    location / {
        root     /home/build/rampage-mobile-front/dist;
        index  index.html index.htm;
      }
}



###if判断重写字段是否跳转

猜你喜欢

转载自blog.csdn.net/weixin_42207486/article/details/82733995
今日推荐