Nginx 怎么配置根据 User-Agent 跳转到不通地址,比如,跳转到移动端的网站 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
请不要在回答技术问题时复制粘贴 AI 生成的内容
assad

Nginx 怎么配置根据 User-Agent 跳转到不通地址,比如,跳转到移动端的网站

  •  
  •   assad Apr 5, 2017 5542 views
    This topic created in 3312 days ago, the information mentioned may be changed or developed.

    Nginx 怎么配置根据 User-Agent 跳转到不通地址
    比如,跳转到移动端的网站

    我怎么配都跳转不过去

    14 replies    2017-04-06 15:42:55 +08:00
    willakira
        1
    willakira  
       Apr 5, 2017
    强烈不推荐这么做,如果是用于生产环境的话
    Nginx 、 apache 、 tomcat 这些 server 的配置一般不是由开发来管理的,因此
    - 每次更新的时候都需要运维参与
    - 出现问题的时候回滚非常麻烦
    - 性能倒是其次, debug 会非常复杂

    一般都会在 container 跑个小程序做这个,好处有
    - 自己更新
    - 出问题了自己回滚
    - 容易 debug

    把难以监控,不透明的东西做的简单,复杂的东西做的透明,就是这样
    sundong
        2
    sundong  
       Apr 5, 2017
    ## rewrite spider
    if ($http_user_agent ~* (baiduspider|googlebot|Googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot)) {
    rewrite ^/(.*)$ https://www.baidu.com permanent;
    }
    luojiyin87
        3
    luojiyin87  
       Apr 5, 2017   2
    chuhemiao
        4
    chuhemiao  
       Apr 5, 2017
    直接上代码不好吗,干吗要 nginx 配置, php 扩展有手机类型,直接判断了,然走那就走那
    SourceMan
        5
    SourceMan  
       Apr 5, 2017
    前端、后端做,都轮不到运维做。。。
    prasanta
        6
    prasanta  
       Apr 5, 2017
    现在网站直接响应式了
    Cabana
        7
    Cabana  
       Apr 5, 2017 via Android
    咋不做响应式呢
    Showfom
        8
    Showfom  
    PRO
       Apr 5, 2017
    @prasanta
    @Cabana 可能楼主觉得响应式浪费流量

    我们也不建议在 nginx 端做这个,后端程序判断才是最好的
    ragnaroks
        9
    ragnaroks  
       Apr 5, 2017
    更多的可能是上面要求 1 天内做完吧.
    改 nginx 配置可能是最快的了.

    location /{
    set $mob 'y';
    if ($http_user_agent ~* "(Android|iPhone|Windows Phone)"){ set $mob "${mob}e"; }
    if ($host != 'm.exp.com'){ set $mob "${mob}s"; }
    if ($mob = "yes"){ rewrite ^/$ http://m.exp.com/$1 last; }
    include /mnt/clouddisk/sync/web/rewrite.conf; #rewrite end
    }
    ragnaroks
        10
    ragnaroks  
       Apr 5, 2017
    不过现在也更推荐用代码控制,
    让用户访问 exp.com,判断后跳转到 pc.exp.com / m.exp.com
    mingyun
        11
    mingyun  
       Apr 5, 2017
    @luojiyin87 nice 支持这么多语言
    ryd994
        12
    ryd994  
       Apr 6, 2017 via Android
    怎么楼上都是用 if 和 rewrite 的呢………
    用 map 和 return 不好吗?
    shew2356
        13
    shew2356  
       Apr 6, 2017
    <script type="text/Javascript">
    var isIosFlatform = function() {
    return navigator.userAgent.match(/(iPad|iPhone)/) ? !0 : !1
    };
    var isAndroidFlatform = function() {
    return navigator.userAgent.match(/(Android)/) ? !0 : !1
    };
    var isMobile = function() {
    return isIosFlatform() || isAndroidFlatform() ? !0 : !1
    };
    if (!isMobile()) {
    window.location.href = "http://www.xxx.com/"
    }
    </script>
    assad
        14
    assad  
    OP
       Apr 6, 2017
    最后不得已,使用了 lua
    About     Help     Advertise     Blog     API     FAQ     Solana     2635 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 54ms UTC 11:57 PVG 19:57 LAX 04:57 JFK 07:57
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86