Nginx 同一个域名自动识别 pc h5

首先设置环境变量

我们先设置变量,通过判断来改变变量的值(注: 我写在server中)

        set $is_mobile false;   # 初始值
        if ( $http_cookie ~* "ACCESS_TERMINAL=mobile" ) {    #判断匹配手机端
            set $is_mobile true;
        }
        if ($http_user_agent ~* (android|ip(ad|hone|od)|kindle|blackberry|windowss(ce|phone))) {    #匹配手机端类型
            set $is_mobile true;
        }
        
        location / {

                proxy_pass http://pc/;
                if ($is_mobile = true) {
                        proxy_pass http://h5;
                }
        }

如果写在Nginx负载均衡处,会出现问题。当我们输入完整的URL时,他会把URL识别为目录。所以我们写在了流量入口处的Nginx反向代理层。

原文地址:https://www.cnblogs.com/xiaogongzi/p/11712508.html