以下是满足要求的 Nginx 配置文件代码,可以将其放在宝塔 Nginx 配置文件中的 server 块中:

map $http_user_agent $qyuc {
    default 0;
    '~*qyuc' 1;
}

server {
    listen       80;
    server_name  example.com;

    if ($qyuc != 1) {
        return 403;
    }

    # rest of the configuration
}

这段代码使用了 Nginx 的 map 语句,将 $user_agent 中包含 'qyuc' 字符串的请求映射为 1,其他请求映射为 0。然后通过 if 语句判断 $qyuc 的值是否为 1,如果不是则返回 403 错误码。注意,在 Nginx 中使用 if 语句有一定的性能影响,因此尽量避免使用 if 语句。

Nginx 配置:使用 map 语句限制 UA 包含 'qyuc' 的访问

原文地址: https://www.cveoy.top/t/topic/nwDR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录