








add_action('init', function() { $uri = $_SERVER['REQUEST_URI']; // 只针对以下路径进行检查 $is_target_page = ( $uri === '/' || strpos($uri, '/product/') !== false || strpos($uri, '/product-category/') !== false || strpos($uri, '/product-tag/') !== false ); if (!$is_target_page) { return; } // 允许的搜索引擎来源 $allowed_referrers = ['google.', 'bing.', 'yahoo.', 'duckduckgo.', 'yandex.', 'brave.']; // 允许的爬虫 UA 关键词$allowed_bots = [ 'google', // 放宽匹配 googlebot / google-inspectiontool / google-speakr 等 'bing', 'yahoo', 'duckduck', 'yandex', 'aol', 'brave' ]; $user_agent = strtolower($_SERVER['HTTP_USER_AGENT'] ?? ''); $referer = strtolower($_SERVER['HTTP_REFERER'] ?? ''); // 放行爬虫 foreach ($allowed_bots as $bot) { if (strpos($user_agent, $bot) !== false) { return; } } //判断是否搜索引擎来源 $has_valid_referrer = false; foreach ($allowed_referrers as $domain) { if (strpos($referer, $domain) !== false) { $has_valid_referrer = true; break; } } // 如果从搜索引擎来,设置 cookie if ($has_valid_referrer) { setcookie('from_search_engine', '1', time() + 86400, '/', '', false, true); return; } // 如果已有 cookie,放行 if (isset($_COOKIE['from_search_engine']) && $_COOKIE['from_search_engine'] === '1') { return; } // 否则拒绝访问 status_header(500); exit;});
3、网页程序限制的方法还有很多,也可以组合使用。比如浏览器的语言,浏览器的Cookies ....都是非常好用的。
location / {allow 192.168.1.100; # 允许访问的IPallow 203.0.113.45;deny all; # 其他禁止}
geoip2 /etc/nginx/geoip/GeoLite2-Country.mmdb {$geoip2_data_country_code country iso_code;}map $geoip2_data_country_code $allowed_country {default no;US yes; # 只允许美国访问CA yes; # 允许加拿大访问}server {if ($allowed_country = no) {return 403;}}
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;server {location / {limit_req zone=one burst=5 nodelay;}}
location / {valid_referers none blocked yoursite.com *.yoursite.com;if ($invalid_referer) {return 403;}}
文章为作者独立观点,不代表DLZ123立场。如有侵权,请联系我们。( 版权为作者所有,如需转载,请联系作者 )
网站运营至今,离不开小伙伴们的支持。 为了给小伙伴们提供一个互相交流的平台和资源的对接,特地开通了独立站交流群。
群里有不少运营大神,不时会分享一些运营技巧,更有一些资源收藏爱好者不时分享一些优质的学习资料。
现在可以扫码进群,备注【加群】。 ( 群完全免费,不广告不卖课!)

发表评论 取消回复