网站图像防盗----Apache配置妙法_PHP
Apache
每个网站所有者都在尽力美化自己的网站,使它看上去更酷、更具有吸引力,其中最常见的方法就是使用图片、Logo及Flash等。但是,这也会带来一个问题,因为越漂亮、越吸引人的网站,漂亮的图片和Flash等就容易被别的网站悄悄的盗用。下面我们就一起讨论如何防止网站图像被盗用。需要解决的问题
简单的说,这里有两种不同的盗用行为:
1. 使用HTML标记IMG在自己的网站中引用网站的图片。
2. 从网站上下载图片,然后放在自己的网站上。
对于第一种的盗用行为,合法网站的图片被用来美化装饰其它网站,这种盗用对合法网站的损害比较大,因为访问非法网站的访问者其实是从合法网站获取图片的,合法网站的日志文件充满了访问请求记录,并且带宽被非法访问消耗,而合法网站却没有得到任何好处。这种类型的盗用通过技术手段完全可以被防止。
第二种类型的盗用相对来说比较阴险,浏览者在非法网站直接访问非法的图片,而合法网站的版权受到侵害,却得不到赔偿,甚至无法发现这种盗用。因为Web的工作方式对这种类型的盗用实际上无法被阻止,但是可以使得这种盗用更加困难。
完全杜绝这两种盗用行为是不现实的,但是通过技术手段可以使得这种盗用非常困难。在Apache环境下,通过配置可以限制网站图片被盗用。
标识需要保护的文件
作为网站管理员,最大的希望就是能够保护网站上所有文档,但是从技术角度考虑这种想法是不现实的,因此我们这里只讨论对图片文件的保护。
作为保护的第一步,首先需要标识出需要保护的文件,然后才能进一步对被标识的文件进行保护。在Apache配置文件中添加如下内容:
<ccid_code><filesmatch> [这里添加保护限制命令] </filesmatch></ccid_code> Copy after login |
将
Referer HTTP头字段
当用户访问Web服务器请求一个页面时,用户浏览器发送的HTTP请求中会有一个被称为HTTP请求头(HTTP Request Header)的信息,这个头信息中包含客户请求的一些信息,例如发出请求客户主机的浏览器版本、用户语言、用户操作系统平台、用户请求的文档名等,这些信息以变量名/变量值的方式被传输。
在这些信息中,Referer字段对于实现防止图片盗用非常重要。Referer字段指定客户端最后一个页面的URL地址。例如,如果用户访问页面A,然后点击在页面A上到页面B的链接,访问页面B的HTTP请求会包括一个Referer字段,该字段会包括这样的信息“这个请求是来自于页面A”。如果一个请求不是来自于某个页面,而是用户通过直接在浏览器地址栏输入页面A的URL地址的方式来访问页面A,那么在HTTP请求中则不会包括Referer字段。这样对于我们防止盗链有什么帮助呢?Referer字段是帮助判断对图像的请求是来自自己的页面,还是来自其它网站。
使用SetEnvIf对图像进行标记
作为一个简单的例子,假设需要保护的网站的主页面为http://my.apache.org,这时候希望限制所有不是源于本网站的网络访问请求(例如只允许访问包含在本网站页面内的图片)。这里可以使用一个环境变量作为一个标记,如果条件满足时就设置该变量,如下所示:
SetEnvIfNoCase Referer "^http://my\.apache\.org/" local_ref=1
当Apache处理一个请求时,它会检查HTTP请求头中的Referer字段,如果该请求来源于本网站(也就是请求页面的URL为本网站域名),则设置环境变量local_ref为1。
在双引号中的字符串是一个正则表达式,只有匹配该正则表达式,环境变量才会被设置。本文不讨论如何使用正则表达式,这里只需要理解SetEnvIf*命令会使用正则表达式作为参数。
SetEnvIfNoCase命令的“NoCase”部分表示这里的正则表达式忽略大小写,'http://my.apache.org/'、'http://My.Apache.Org/'或 'http://MY.APACHE.ORG/'都可以匹配条件。
在访问控制中使用环境变量
Apache配置文件中的Order、Allow和Deny命令可以实现对文档的基于环境变量的访问控制,使用Order、Allow和Deny命令首先要考虑的是Allow和Deny命令的顺序对于Apache处理结果的影响,应该以下面的方式使用:
Order Allow,Deny
这里表示Apache首先处理该HTTP请求相关的Allow命令,然后处理相关的Deny命令。这种处理方式的默认策略是Deny,所以除非有明确的允许的设置,否则该请求就会被拒绝,任何非法访问将无法成功。
因此,在Apache的配置文件httpd.conf中添加如下命令,来实现本地引用发挥作用:
<ccid_code>Order Allow,Deny Allow from env=local_ref</ccid_code> Copy after login |
这样只有在local_ref变量被定义的情况下,该请求才会被允许;否则其它所有请求和访问将会被拒绝,因为这些请求不满足Allow条件。
注意,请不要在.htaccess和httpd.conf中使用
把这些相关设置放在一起,在Apache的配置文件中就会有如下内容:
<ccid_code>SetEnvIfNoCase Referer "^http://my\.apache\.org/" local_ref=1 <filesmatch> Order Allow,Deny Allow from env=local_ref </filesmatch></ccid_code> Copy after login |
如上配置可以存放在服务器配置文件httpd.conf中,或者存放在.htaccess文件中,最后的效果是一样的:在这些命令作用的范围内,只有从本网站引用的图片才可以被访问。
对图片进行水印处理
上面介绍的方法并不能完全防止图像盗链,这是因为有些执著的盗用者可以伪造Referer值来盗用图片,使相关设置失效,所以不可能完全防止网站图片被盗链,但是上面采取的措施会使得盗链变得很困难。
此外,还有一个防止图片被盗用的方法,就是对网站的图片都进行水印处理。对一个数字图片进行水印处理是指在图片中加入一个特殊的签名编码,并且可以进行验证和检测,数字水印并不会降低图片的质量,甚至可以实现图像被切割以后的剩余部分仍然会包括水印信息。图片被再次编辑、打印,并再次扫描以后,水印仍然可以被检测到。因此,水印技术是一个非常好的保护图片不被盗用的技术。
记录盗用请求
如果想知道自己网站的艺术品是否被盗,可以尝试使用同样的侦测和环境变量来记录可疑请求。例如,在httpd.conf文件中添加如下命令,那么会在/usr/local/web/apache/logs/poachers_log文件中记录所有具有非法的Referer头信息的访问请求:
<ccid_code>SetEnvIfNoCase Referer "!^http://my\.apache\.org/" not_local_ref=1 SetEnvIfNoCase Request_URI "\.(gif|jpg)" is_image=1 RewriteEngine On RewriteCond ${ENV:not_local_ref} =1 RewriteCond ${ENV:is_image} =1 RewriteRule .* - [Last,Env=poach_attempt:1] CustomLog logs/poachers_log CLF env=poach_attempt</ccid_code> Copy after login |
在上面代码中,头两行为条件设置标记(也就是没有正确的本地Referer的图片文件),RewriteCond检测是否该标记被设置,然后RewriteRule设置第三个标记,最后一行使得这样的访问请求被记录在特定的文件中。
上面简单介绍了在Apache环境下,如何通过配置来限制网站图片被盗用的方法,抛砖引玉,希望大家将自己更好的经验介绍出来。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

With the popularity of Douyin short videos, user interactions in the comment area have become more colorful. Some users wish to share images in comments to better express their opinions or emotions. So, how to post pictures in TikTok comments? This article will answer this question in detail and provide you with some related tips and precautions. 1. How to post pictures in Douyin comments? 1. Open Douyin: First, you need to open Douyin APP and log in to your account. 2. Find the comment area: When browsing or posting a short video, find the place where you want to comment and click the "Comment" button. 3. Enter your comment content: Enter your comment content in the comment area. 4. Choose to send a picture: In the interface for entering comment content, you will see a "picture" button or a "+" button, click

Title: The working principle and configuration method of GDM in Linux systems In Linux operating systems, GDM (GNOMEDisplayManager) is a common display manager used to control graphical user interface (GUI) login and user session management. This article will introduce the working principle and configuration method of GDM, as well as provide specific code examples. 1. Working principle of GDM GDM is the display manager in the GNOME desktop environment. It is responsible for starting the X server and providing the login interface. The user enters

In PowerPoint, it is a common technique to display pictures one by one, which can be achieved by setting animation effects. This guide details the steps to implement this technique, including basic setup, image insertion, adding animation, and adjusting animation order and timing. Additionally, advanced settings and adjustments are provided, such as using triggers, adjusting animation speed and order, and previewing animation effects. By following these steps and tips, users can easily set up pictures to appear one after another in PowerPoint, thereby enhancing the visual impact of the presentation and grabbing the attention of the audience.

Apple's recent iPhones capture memories with crisp detail, saturation and brightness. But sometimes, you may encounter some issues that may cause the image to look less clear. While autofocus on iPhone cameras has come a long way, allowing you to take photos quickly, the camera can mistakenly focus on the wrong subject in certain situations, making the photo blurry in unwanted areas. If your photos on your iPhone look out of focus or lack sharpness overall, the following post should help you make them sharper. How to Make Pictures Clearer on iPhone [6 Methods] You can try using the native Photos app to clean up your photos. If you want more features and options

Some netizens found that when they opened the browser web page, the pictures on the web page could not be loaded for a long time. What happened? I checked that the network is normal, so where is the problem? The editor below will introduce to you six solutions to the problem that web page images cannot be loaded. Web page images cannot be loaded: 1. Internet speed problem The web page cannot display images. It may be because the computer's Internet speed is relatively slow and there are more softwares opened on the computer. And the images we access are relatively large, which may be due to loading timeout. As a result, the picture cannot be displayed. You can turn off the software that consumes more network speed. You can go to the task manager to check. 2. Too many visitors. If the webpage cannot display pictures, it may be because the webpages we visited were visited at the same time.

When using WPS office software, we found that not only one form is used, tables and pictures can be added to the text, pictures can also be added to the table, etc. These are all used together to make the content of the entire document look richer. , if you need to insert two pictures into the document and they need to be arranged side by side. Our next course can solve this problem: how to place two pictures side by side in a wps document. 1. First, you need to open the WPS software and find the picture you want to adjust. Left-click the picture and a menu bar will pop up, select "Page Layout". 2. Select "Tight wrapping" in text wrapping. 3. After all the pictures you need are confirmed to be set to "Tight text wrapping", you can drag the pictures to the appropriate position and click on the first picture.

Are you also using Foxit PDF Reader software? So do you know how Foxit PDF Reader converts pdf documents into jpg images? The following article brings you how Foxit PDF Reader converts pdf documents into jpg images. For those who are interested in the method of converting jpg images, please come and take a look below. First start Foxit PDF Reader, then find "Features" on the top toolbar, and then select the "PDF to Others" function. Next, open a web page called "Foxit PDF Online Conversion". Click the "Login" button on the upper right side of the page to log in, and then turn on the "PDF to Image" function. Then click the upload button and add the pdf file you want to convert into an image. After adding it, click "Start Conversion"
