Nginx 设置屏蔽PC端,苹果,或者安卓的访问请求吗?

共以下 3 个回答

  • LinkCode 永久会员 2022年10月7日 下午5:30

    需求说明

    接开发的需求:

    1.屏蔽所有pc端的请求

    2.安卓移动端不做限制,访问原链接

    3.苹果移动设备跳转到指定的推广页面

    Nginx配置

    首先想到的就是根据user_agent去判断请求链接来自移动还是pc,pc直接返回404即可,iPhone做rewrite跳转。而安卓不需要做操作,折腾了一个多小时,开始被安卓绕住了,最终规则如下:#假设跳转到baidu页面

    if ($http_user_agent ~* “iPhone|ipad”){
    rewrite ^/(.*)$ http://www.baidu.com permanent;

    }

    #如果不是安卓也不是苹果,那就认为是PC端请求

    if ($http_user_agent ~* “Android|iPhone|ipad”){
    return 404;

    }

    如果是针对”.apk”后缀的文件做如上操作,方法如下:location ~ .*\.apk$ {
    if ($http_user_agent ~* “iPhone|ipad”){
    rewrite ^/(.*)$ http://www.baidu.com permanent;

    }

    if ($http_user_agent ~* “Android|iPhone|ipad”){
    return 404;

    }

    }

    0 赞同 0 条回复

# 回答此问题

您的电子邮箱地址不会被公开。 必填项已用*标注