惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
C
Cybersecurity and Infrastructure Security Agency CISA
G
Google Developers Blog
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
TaoSecurity Blog
TaoSecurity Blog
V
Visual Studio Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
博客园 - Franky
S
Security @ Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
T
Troy Hunt's Blog
B
Blog RSS Feed
A
About on SuperTechFans
IT之家
IT之家
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
J
Java Code Geeks
S
Securelist
T
Threatpost
SecWiki News
SecWiki News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
Docker
N
News and Events Feed by Topic
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
罗磊的独立博客
H
Hacker News: Front Page
N
News and Events Feed by Topic
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
P
Privacy International News Feed
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
Vulnerabilities – Threatpost
The Register - Security
The Register - Security

Hyde Blog

Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog 关于我 关于我 Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog Hyde Blog
Hyde Blog
Hyde · 2025-10-05 · via Hyde Blog

Nginx实现图片防盗链 ​

前言 ​

什么是图片盗链

首先我们聊聊什么是图片盗链?

盗链指的是其他网站不经过你的许可直接通过链接使用你网站上的资源,从而消耗你的带宽和资源,这不仅会影响你网站的性能,还有可能会导致资源被滥用。

简单来说,直接从你的网站上右键复制链接,粘贴到他自己的网站使用,实际请求的是你的服务器。

今天我们就来说说nginx如何实现配置静态资源服务器及防盗链实现。

警告

图片你直接在浏览器打开用可以,但是如果图片 url 出现在你的网站里(img 标签里),将是不被允许的。

版权 ​

实现 ​

  • 首先准备一张防盗链提示图片(来源于百度),盗链提示图片

    1741447799552

  • nginx 安装目录的 html 下创建 res 目录,将上述图片上传至此,可以直接访问到,将此图片作为盗链提示

    sh

    # 创建资源目录并上传图片,/usr/share/nginx/是我云服务器Nginx的安装目录
    sudo mkdir -p /usr/share/nginx/html/res
  • 防盗链配置位置

  • 错误页面配置放在server块中

    提示 此配置是在图床数据下进行配置,以实际为准!我的是在/etc/nginx/conf.d/RuyuBlog.conf里面进行配置的

    nginx

    server {
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        server_name seasir.top;
    
        root /root/home3.0;
    
        #省略...
    
        # 图床数据
        location /images {
            alias /images;
            index index.html;
        }
    
        location /error {
            autoindex on; # 启用目录索引
            alias /FdangDaoLianImages;
            index index.html;   # 默认显示index.html文件,如果没有则列出目录内容
        }
    
        # 引入 SSL 公用参数
        include /etc/nginx/common_configs/common_ssl_params.conf;
    }
  • 在服务器根目录下创建/FdangDaoLianImages/文件夹并进入此目录

    sh

    #c创建此目录
    [root@VM-4-16-centos res]# mkdir /FdangDaoLianImages
    #进入此目录
    [root@VM-4-16-centos res]# cd /FdangDaoLianImages/
    #rz表示从电脑本地开始上传
    [root@VM-4-16-centos FdangDaoLianImages]# rz
    #或者
    sudo mv /usr/share/nginx/html/res/1.jpg /FdangDaoLianImages/
    [root@VM-4-16-centos FdangDaoLianImages]# ll
    #上传防盗链提示图片
    -rw-r--r-- 1 root root 150863 3月   8 23:45 1.jpg
  • 重启服务

    sh

    systemctl restart nginx
  • 验证:https://seasir.top/error/1.jpg

配置 Nginx ​

我的图床图片资源存放于服务器的/images目录下

提示 此配置是在图床数据下进行配置,以实际为准!我的是在/etc/nginx/conf.d/RuyuBlog.conf里面进行配置的

nginx

#vim /etc/nginx/conf.d/RuyuBlog.conf
……
server {
     # 监听端口及其他配置项......

     # 图床数据
        location /images {
            alias /images;
            index index.html;
            valid_referers none blocked *.seasir.top seasir.top localhost 127.0.0.1;
            if ( $invalid_referer ) {
                # 如不满足,指定访问如下资源
                rewrite ^/ https://seasir.top/error/1.jpg;
                return 403;
            }
        }
     # 其他配置项......
}
……

警告

.seasir.top seasir.top换成实际自己的主域名 https://seasir.top/error/1.jpg换成自己的,注意后缀

  • 参数说明:

    • valid_referers none | blocked | server_names | strings ….;

    • --none:允许没有 http_refer 的请求访问资源,检测 Referer 头域不存在的情况,则可以访问。

    • --blocked:检测 Referer 头域的值被防火墙或者代理服务器删除或伪装的情况。

    • --server_names :只允许指定 ip/域名来的请求访问资源(白名单)。可设置一个或多个 URL ,检测 Referer 头域的值是否是这些 URL 中的某一个。在生产环境中尽量使用域名,不使用 ip。

  • 重启服务

sh

  systemctl restart nginx