nginx 默认启用 ngx_http_referer_module,可通过 nginx -v | grep with-http_referer_module 确认;若被禁用需重新编译并移除 --without-http_referer_module 参数;该模块属核心模块,支持 valid_referers 等指令,但 referer 可伪造,仅适用于基础防盗链。

Nginx 默认已启用 ngx_http_referer_module,无需额外操作即可使用 valid_referers 等指令。
如果你执行 nginx -V 查看编译参数,大概率会看到类似输出:
configure arguments: --prefix=/usr/local/nginx ...
里面不包含 --without-http_referer_module —— 这说明模块已被编译进二进制文件,直接可用。
只有当你明确在编译时加了 --without-http_referer_module,该模块才会被禁用。
确认 Referer 模块是否启用
运行以下命令检查:
nginx -V 2>&1 | grep -o with-http_referer_module
- 若输出
with-http_referer_module→ 模块已启用 ✅ - 若无输出,再执行:
nginx -V 2>&1 | grep -o without-http_referer_module
- 若输出
without-http_referer_module→ 模块被显式禁用 ❌,需重新编译。
如需手动启用(即曾被禁用后恢复)
重新编译 Nginx 时,不要加 --without-http_referer_module,保持默认即可:
./configure \ --prefix=/usr/local/nginx \ --with-http_ssl_module \ # 其他你需要的模块... # 不要写 --without-http_referer_module
然后:
make && make install
编译完成后,valid_referers、$invalid_referer 等功能即可在 server 或 location 块中正常使用。
补充说明
- 该模块不依赖第三方库,属于核心 HTTP 模块之一;
- 配置时只需在
location中写:
location ~* \.(jpg|png|gif|css|js)$ {
valid_referers none blocked server_names *.example.com;
if ($invalid_referer) {
return 403;
}
}
- 注意:
Referer可被客户端伪造,它适合做基础防护,不能替代鉴权或签名机制(如secure_link)。
不复杂但容易忽略











