nginx--防盗链

盗链

通过在自己网站里面'引用别人的资源链接','盗用'人家的劳动和资源

referer

referer是记录打开一个页面之前记录是从哪个页面跳转过来的标记信息

正常的referer信息

none:请求报文首部没有referer首部,比如用户直接在浏览器输入域名访问web网站,就没有referer信息。

blocked:请求报文有referer⾸首部,但无有效值,比如为空。

server_names:referer首部中包含本主机名及即nginx 监听的server_name。

arbitrary_string:自定义指定字符串,但可使用*作通配符。

regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,例如:

~.*\.magedu\.com。

实现web盗链

配置nginx.conf文件

复制代码
    log_format access_json '{"@timestamp":"$time_iso8601",'
        '"host":"$server_addr",'
        '"clientip":"$remote_addr",'
        '"size":$body_bytes_sent,'
        '"responsetime":$request_time,'
        '"upstreamtime":"$upstream_response_time",'
        '"upstreamhost":"$upstream_addr",'
        '"http_host":"$host",'
        '"uri":"$uri",'
        '"domain":"$host",'
        '"xff":"$http_x_forwarded_for",'
        '"referer":"$http_referer",'
        '"tcp_xff":"$proxy_protocol_addr",'
        '"http_user_agent":"$http_user_agent",'
        '"status":"$status"}';
    access_log  logs/access.log  access_json;

    server {
        listen       80;
        server_name  www.xfxq.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /data/nginx/html;
            index  index.html index.htm;
        }

配置index.html文件

复制代码
<html>
<body>
<meta charset="utf-8">
<a href="http://www.fxq.com">盗链</a>
<img src="http://www.fxq.com/1.jpg">
</body>
</html>
</html>

查看图片

查看日志

实现防盗链

http:// https://nginx.org/en/docs/http/ngx_http_referer_module.html#valid_referers

格式

复制代码
valid_referers none blocked server_names
               *.example.com example.* www.example.org/galleries/ 
               ~\.google\.; 定义有效的referer

if ($invalid_referer) {  如是使用其他的无效的referer访问:
    return 403;  返回状态码403
}  

定义防盗链

复制代码
server {
  listen 80;
  listen       443 ssl;
  ssl_certificate  /apps/nginx/certs/www.fxq.com.crt;
  ssl_certificate_key /apps/nginx/certs/www.fxq.com.key;
  ssl_session_cache shared:sslcache:20m;
  ssl_session_timeout 10m;
  server_name www.fxq.com;
  location / {
     root /data/nginx/html/pc;
     index index.html;
    }
  location = /favicon.ico {
     root /data/nginx/html/pc;
  }
  location = /1.jpg{
     root /data/nginx/static; 
     valid_referers none blocked server_names
                  ~\.google\.; 
     if ($invalid_referer) {  
             return 403;
       }  
    }
  }    
[ro

访问结果

相关推荐
左手厨刀右手茼蒿13 小时前
Flutter 三方库 firebase_admin 跨云边管线企业级鸿蒙管控底座适配风云:无障碍贯穿服务器授权防火墙打通底层生态授权域并构建海量设备推送集结-适配鸿蒙 HarmonyOS ohos
服务器·flutter·harmonyos
钛态13 小时前
Flutter for OpenHarmony:shelf_web_socket 快速构建 WebSocket 服务端,实现端到端实时通信(WebSocket 服务器) 深度解析与鸿蒙适配指南
服务器·前端·websocket·flutter·华为·性能优化·harmonyos
星辰徐哥13 小时前
C++网络编程:TCP服务器与客户端的实现
网络·c++·tcp/ip
初九之潜龙勿用13 小时前
C# 解决“因为算法不同,客户端和服务器无法通信”的问题
服务器·开发语言·网络协议·网络安全·c#
杨柳轻扬13 小时前
从0开始搭建泰拉瑞亚tModLoader服务器(Linux)
linux·服务器·游戏
星辰徐哥13 小时前
C语言网络编程:TCP/IP协议栈、套接字、服务器/客户端通信深度解析
c语言·网络·tcp/ip
祢真伟大13 小时前
dmagent-2026监控服务器代理部署
运维·服务器
算法-大模型备案 多米14 小时前
大模型备案实操指南:材料、流程与避坑要点
大数据·网络·人工智能·算法·文心一言
十年编程老舅14 小时前
窥探内核心脏:深入解析 proc 虚拟文件系统
linux·服务器·数据库·c++·linux内核·文件系统·读写锁
LSL666_14 小时前
云服务上安装nginx
java·运维·nginx