V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
assad
V2EX  ›  程序员

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

  •  
  •   assad · 2017-04-05 11:36:31 +08:00 · 4906 次点击
    这是一个创建于 2577 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    我怎么配都跳转不过去

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

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

    把难以监控,不透明的东西做的简单,复杂的东西做的透明,就是这样
    sundong
        2
    sundong  
       2017-04-05 13:39:08 +08:00
    ## 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  
       2017-04-05 14:27:05 +08:00   ❤️ 2
    chuhemiao
        4
    chuhemiao  
       2017-04-05 14:32:22 +08:00
    直接上代码不好吗,干吗要 nginx 配置, php 扩展有手机类型,直接判断了,然走那就走那
    SourceMan
        5
    SourceMan  
       2017-04-05 14:33:13 +08:00
    前端、后端做,都轮不到运维做。。。
    prasanta
        6
    prasanta  
       2017-04-05 14:37:12 +08:00
    现在网站直接响应式了
    Cabana
        7
    Cabana  
       2017-04-05 18:36:29 +08:00 via Android
    咋不做响应式呢
    Showfom
        8
    Showfom  
       2017-04-05 19:05:12 +08:00
    @prasanta
    @Cabana 可能楼主觉得响应式浪费流量

    我们也不建议在 nginx 端做这个,后端程序判断才是最好的
    ragnaroks
        9
    ragnaroks  
       2017-04-05 19:56:12 +08:00
    更多的可能是上面要求 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  
       2017-04-05 19:58:19 +08:00
    不过现在也更推荐用代码控制,
    让用户访问 exp.com,判断后跳转到 pc.exp.com / m.exp.com
    mingyun
        11
    mingyun  
       2017-04-05 22:32:18 +08:00
    @luojiyin87 nice 支持这么多语言
    ryd994
        12
    ryd994  
       2017-04-06 07:56:48 +08:00 via Android
    怎么楼上都是用 if 和 rewrite 的呢………
    用 map 和 return 不好吗?
    shew2356
        13
    shew2356  
       2017-04-06 09:54:47 +08:00
    <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
       2017-04-06 15:42:55 +08:00
    最后不得已,使用了 lua
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3560 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:59 · PVG 18:59 · LAX 03:59 · JFK 06:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.