stevie-ray/referrer-spam-blocker 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stevie-ray/referrer-spam-blocker

Composer 安装命令:

composer require stevie-ray/referrer-spam-blocker

包简介

Apache, Nginx, IIS, uWSGI, Varnish, HAProxy & Lighttpd blacklist plus Google Analytics segment to prevent referrer spam traffic

README 文档

README

Apache, Nginx, IIS, uWSGI, Caddy, Varnish, HAProxy & Lighttpd blacklist + Google Analytics segments to prevent referrer spam traffic


Latest Stable Version Build Status Libraries.io dependency status for latest release Code Quality Packagist License

Apache: .htaccess

.htaccess is a configuration file for use on web servers running Apache. This file is usually found in the root “public_html” folder of your website. The .htaccess file uses two modules to prevent referral spam, mod_rewrite and mod_setenvif. Decide which method is most suitable with your Apache server configuration. This file is Apache 2.4 ready, where mod_authz_host got deprecated.

Nginx: referral-spam.conf

IMPORTANT: You must increase the map hash bucket size to support the large domain list. With referral-spam.conf in /etc/nginx, include it globally from within /etc/nginx/nginx.conf:

http {
	map_hash_bucket_size 128;
	include referral-spam.conf;
}

Add the following to each /etc/nginx/site-available/your-site.conf that needs protection:

server {
	if ($bad_referer) {
		return 444;
	}
}

Performance Note: This configuration uses a performance-optimized approach with hostname matching instead of thousands of regex patterns. Only one regex is evaluated per request to extract the domain from the Referer header, significantly improving NGINX performance compared to traditional regex-based blocking methods.

Varnish: .refferal-spam.vcl

Add referral-spam.vcl to Varnish 4 default file: default.vcl by adding the following code right underneath your default backend definitions

include "referral-spam.vcl";
sub vcl_recv { call block_referral_spam; }

IIS (Internet Information Services): web.config

The web.config file is located in the root directory of your Windows Server web application.

Caddy (HTTP/2 Web Server with Automatic HTTPS): referral-spam.caddy and referral-spam.caddy2

Move this file next to your Caddy config file, and include it by doing:

# For Caddy 1:
 include ./referral-spam.caddy;
# For Caddy 2:
 import ./referral-spam.caddy2

Then start your caddy server. All the referrers will now be redirected to a 444 HTTP answer

uWSGI: referral_spam.res

Include the file referral_spam.res into your vassal .ini configuration file:

ini = referral_spam.res:blacklist_spam

HAProxy: referral-spam.haproxy

Use it in your HAProxy config by adding all domains.txt items, in any frontend, listen or backend block:

acl spam_referer hdr_sub(referer) -i -f /etc/haproxy/referral-spam.haproxy
http-request deny if spam_referer

Lighttpd: referral-spam.lighttpd.conf

Include this file in your main lighttpd.conf:

include "referral-spam.lighttpd.conf"

Make sure mod_rewrite is enabled in your server.modules:

server.modules = ("mod_rewrite", ...)

The configuration blocks referrer spam by redirecting requests with spam referrers. For better performance with large domain lists, consider using mod_magnet.

OpenLiteSpeed: .htaccess

OpenLiteSpeed is Apache-compatible and supports .htaccess files. Simply use the Apache .htaccess file (see Apache section above).

Make sure mod_rewrite is enabled in your OpenLiteSpeed configuration:

  • Admin Panel > Server > Modules > mod_rewrite (enable)

Options for Google Analytics 'ghost' spam

The above methods don't stop the Google Analytics ghost referral spam (because they are hitting Analytics directly and don't touching your website). You should use filters in Analytics to prevent ghost referral spam and hide spam form the past. Because Google Analytics segments are limited to 30.000 characters the exclude list is separated into multiple parts.

Navigate to your Google Analytics Admin panel and add these Segments:

Filter Session Include
Hostname matches regex ```your-website.com
Filter Session Exclude
Source matches regex Copy all the domains from google-exclude-1.txt to this field

Do the same for google-exclude-2.txt. Please note there may be more files in the future.

You can also prevent ghost referral spam by:

Command Line Interface

# Basic usage
php run.php
php run.php --types apache,nginx
php run.php --dry-run
php run.php --output /path/to/configs

# Options: -h (help), -v (version), --dry-run, -o (output), -t (types)
# Supported types: apache, nginx, varnish, iis, uwsgi, caddy, caddy2, haproxy, lighttpd, google

Testing

The project includes comprehensive testing and code quality tools:

# Run tests
composer test
composer test-coverage

# Code quality
composer phpstan
composer phpcs
composer phpcbf
composer quality

Tests cover unit testing, configuration generation, domain processing, and file operations. Quality tools include PHPStan (Level 8), PHP CodeSniffer (PSR-12), and Psalm for static analysis.

Programmatic Usage

use StevieRay\Generator;

$generator = new Generator('/path/to/output');
$generator->generateFiles();
$generator->generateSpecificConfigs(['apache', 'nginx']);
$stats = $generator->getStatistics();

Intregrate in a Dockerfile

You can also integrate these configuration file in your Docker repo, so you will get always the most updated version when you build your image. For Apache, Nginx, Varnish 4 or IIS add the following line to your Dockerfile

# Apache: Download .htaccess to /usr/local/apache2/htdocs/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/.htaccess /usr/local/apache2/htdocs/

# Nginx: Download referral-spam.conf to /etc/nginx/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.conf /etc/nginx/

# Varnish 4: Download referral-spam.vcl to /etc/varnish/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.vcl /etc/varnish/

# IIS: Download web.config to /sitepath/ (change sitepath accordingly)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/web.config /sitepath/

# Caddy: Download referral-spam.caddy to /sitepath/ (next to your Caddy config file given through -conf)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.caddy /sitepath/

# uWSGI: Download referral_spam.res to /sitepath/ (change sitepath accordingly)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral_spam.res /sitepath/

# HAProxy: Download referral-spam.haproxy to /etc/haproxy/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.haproxy /etc/haproxy/

# Lighttpd: Download referral-spam.lighttpd.conf to /etc/lighttpd/
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/referral-spam.lighttpd.conf /etc/lighttpd/

# OpenLiteSpeed: Use the Apache .htaccess file (OpenLiteSpeed is Apache-compatible)
ADD https://raw.githubusercontent.com/Stevie-Ray/referrer-spam-blocker/master/.htaccess /sitepath/

Like it?

stevie-ray/referrer-spam-blocker 适用场景与选型建议

stevie-ray/referrer-spam-blocker 是一款 基于 VCL 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 372, 最近一次更新时间为 2016 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「analytics」 「varnish」 「spam」 「apache」 「haproxy」 「lighttpd」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 stevie-ray/referrer-spam-blocker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stevie-ray/referrer-spam-blocker 我们能提供哪些服务?
定制开发 / 二次开发

基于 stevie-ray/referrer-spam-blocker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 50
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 373
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 372
  • Watchers: 50
  • Forks: 84
  • 开发语言: VCL

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-15