1、假设充许连结图片的主机域名为:www.test.com
2、修改httpd.conf
SetEnvIfNoCase Referer "^http://www.test.com/" local_ref=1
<FilesMatch ".(gif|jpg)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
修改还可以防止任意文件盗链下载的问题。
使用以上的方法当从非指定的主机连结图片时,图片将无法显示,如果希望显示一张“禁止盗链”的图片,我们可以用mod_rewrite 来实现。
首先在安装 apache 时要加上 --enable-rewrite 参数加载 mod_rewrite 模组。
假设“禁止盗链”的图片为abc.gif,我们在 httpd.conf 中可以这样配置:
RewriteEngine on
RewriteCond %{ HTTP_REFERER } !^$
RewriteCond %{ HTTP_REFERER } !^http://(www\.)?test.com /.*$ [NC]
RewriteRule \.(gif|jpg)$
[R,L] 当主机的图片被盗链时,只会看到 abc.gif 这张“禁止盗链”的图片!
另:
######## Preventing Image 'Theft' ########
SetEnvIfNoCase Referer “^http://(.)+\.fjhr\.com/” local_ref=1
SetEnvIfNoCase Referer “^http://(.)+\.hzmjp\.com/” local_ref=1
SetEnvIfNoCase Referer “^http://(.)+\.dalouis\.com/” local_ref=1
SetEnvIfNoCase Referer “^http://(.)+\.necktie\.gov\.cn/” local_ref=1
SetEnvIfNoCase Referer “^http://(.)+\.necktie\.net\.cn/” local_ref=1
SetEnvIfNoCase Referer “-” local_ref=1
######## Allow the LOGO image Theft ##########
SetEnvIf Request_URI “/images/logo(.)+” local_ref=0
<FilesMatch “\.(png|gif|jpg)”>
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
1、假设充许连结图片的主机域名为:www.demo.com.cn
2、修改httpd.conf
Code:
SetEnvIfNoCase Referer "^http://www.demo.com.cn/" local_ref=1
<FilesMatch ".(gif|jpg)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
这个简单的应用不光可以解决图片盗链的问题,稍加修改还可以防止任意文件盗链下载的问题。
使用以上的方法当从非指定的主机连结图片时,图片将无法显示,如果希望显示一张“禁止盗链”的图片,我们可以用mod_rewrite 来实现。
首先在安装 apache 时要加上 --enable-rewrite 参数加载 mod_rewrite 模组。
假设“禁止盗链”的图片为abc.gif,我们在 httpd.conf 中可以这样配置:
Code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?demo.com.cn /.*$ [NC]
RewriteRule \.(gif|jpg)$
[R,L] ServerRoot "/Apache"
#可以用绝对路径 e:\web\apache
#ServerRoot用于指定守护进程httpd的运行目录,httpd在启动之后将自动将进程的当前目录改变为这个目录,因此如果设置文件中指定的文件或目录是相对路径,那么真实路径就位于这个ServerRoot定义的路径之下。
PidFile logs/httpd.pid
#PidFile指定的文件将记录httpd守护进程的进程号,由于httpd能自动复制其自身,因此系统中有多个httpd进程,但只有一个进程为最初启动的进程,它为其他进程的父进程,对这个进程发送信号将影响所有的httpd进程。PidFILE定义的文件中就记录httpd父进程的进程号
Timeout 300
#Timeout定义客户程序和服务器连接的超时间隔,超过这个时间间隔(秒)后服务器将断开与客户机的连接.
KeepAlive On
#在HTTP 1.0中,一次连接只能作传输一次HTTP请求,而KeepAlive参数用于支持HTTP 1.1版本的一次连接、多次传输功能,这样就可以在一次连接中传递多个HTTP请求。虽然只有较新的浏览器才支持这个功能,但还是打开使用这个选项
MaxKeepAliveRequests 100
#MaxKeepAliveRequests为一次连接可以进行的HTTP请求的最大请求次数。将其值设为0将支持在一次连接内进行无限次的传输请求。事实上没有客户程序在一次连接中请求太多的页面,通常达不到这个上限就完成连接了。
KeepAliveTimeout 15
#KeepAliveTimeout测试一次连接中的多次请求传输之间的时间,如果服务器已经完成了一次请求,但一直没有接收到客户程序的下一次请求,在间隔超过了这个参数设置的值之后,服务器就断开连接。
<IfModule mpm_winnt.c>
ThreadsPerChild 250
MaxRequestsPerChild 0
</IfModule>
Listen 80
#端口,启动模块.下面许多模块只有在LIUNX下才有用
LoadModule access_module modules/mod_access.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_module modules/mod_auth.so
LoadModule dir_module modules/mod_dir.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
#LoadModule env_module modules/mod_env.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule headers_module modules/mod_headers.so
#LoadModule imap_module modules/mod_imap.so
#LoadModule auth_anon_module modules/mod_auth_anon.so
#LoadModule auth_dbm_module modules/mod_auth_dbm.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule include_module modules/mod_include.so
#LoadModule info_module modules/mod_info.so
#LoadModule isapi_module modules/mod_isapi.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule speling_module modules/mod_speling.so
#LoadModule status_module modules/mod_status.so
#LoadModule unique_id_module modules/mod_unique_id.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule ssl_module modules/mod_ssl.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#LoadModule userdir_module modules/mod_userdir.so
LoadModule rewrite_module modules/mod_rewrite.so
#ExtendedStatus On
ServerAdmin abc@123.com
#服务器管理员邮箱,在出错,探针中显示
ServerName localhost.domain.com
UseCanonicalName Off
#是否允许对每个访问者的 DNS 域名的反向查询
DocumentRoot "/Apache"
#放网页的目录.这个很重要.
#你设置了,e:\web是目录 而虚拟主机设置了e:\webtwo
#虽然程序还是能运行,但是不在documentroot内的话,程序会有错误
<Directory />
Options FollowSymLinks MultiViews ExecCGI
AllowOverride None
</Directory>
<Directory "/Apache/">
Options FollowSymLinks MultiViews ExecCGI
AllowOverride None
Order allow,deny
php_admin_value open_basedir /apache/
Allow from all
</Directory>
#以下是关于用户目录配置部分,即用户可以用http://www.xxxx.com/~username的方式来访问自已的网站!
#UserDir /apache/webroot/aaausersfreehost/*/public_html
#<Directory e:/虚拟主机/*/public_html>
# AllowOverride None
# Options MultiViews SymLinksIfOwnerMatch IncludesNoExec ExecCGI
#下面这条指令定义php只能对public_html此目录及其子目录下的文件进行操作.
# php_flag engine off #表示此目录下的PHP文件不被解释执行,限没有PHP权限。
# php_admin_value open_basedir ./
# Order allow,deny
# Allow from all
#</Directory>
#下面这条指令可以把虚拟目录freehost映射到实际路径/apache/webroot/users/下!
#AliasMatch ^/class/([0-9]).html$ /article.php?articleid=
#AliasMatch ^/class/([0-9]+).([0-9a-zA-Z]+)\.html$ /article.php?articleid=&pagenum=
DirectoryIndex index.html index.htm index.php index.cgi bbs.pl index.pl admin.php login.htm login.html login.php
#默然主机头
AccessFileName .htaccess
<Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
TypesConfig conf/mime.types
DefaultType text/plain
<IfModule mod_mime_magic.c>
MIMEMagicFile conf/magic
</IfModule>
#以下是限制主机同时并发连接数
#<IfModule prefork.c>
#ServerLimit 2000
#StartServers 15
#MinSpareServers 15
#MaxSpareServers 20
#MaxClients 850 #最大客户端
#MaxRequestsPerChild 10000 #最大请求数
#</IfModule>
#以下是对单个文件进行访问控制
#<Files ~ "要屏蔽的文件的名称">
#Order allow,deny
#Allow from 允许IP
#Deny from all
#</Files>
HostnameLookups Off
#EnableMMAP off
#EnableSendfile off
ErrorLog logs/error.log
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access.log common
#CustomLog logs/referer.log referer
#CustomLog logs/agent.log agent
#CustomLog logs/access.log combined
ServerTokens Full
ServerSignature Off
#当客户请求的网页并不存在时,服务器将产生错误文档,缺省情况下由于打开了 ServerSignature选项,错误文档的最后一行将包含服务器的名字、Apache的版本等信息。
#Alias /class/ "/apache/webroot/phpa" #虚拟目录啦
# <Directory /apache/phpsafe> #对虚拟目录进行权限配置
# Options FollowSymLinks #+MultiViews ExecCGI为有CGI执行权限
# php_admin_value safe_mode 1 #表示此目录下的PHP脚本只能有php_safe下权限
# php_admin_value open_basedir /apache/phpsafe/
# 此句是限制PHP的文件操作只能限于/apache/phpsafe及其子目录下,不能对此目录外的文进行操作
# php_flag engine off
#表示此目录下的PHP脚本不会被解释执行,此选项要求PHP必须以模块方式安装的!
# AllowOverride None
# Order allow,deny
# Allow from all #表示允许任何人从任何地方访问
# </Directory>
#ScriptAlias /cgi-bin/ "E:/Apache2/cgi-bin/"
#<Directory "E:/Apache2/cgi-bin">
# AllowOverride None
# Options None
# Order allow,deny
# Allow from all
#</Directory>
IndexOptions FancyIndexing VersionSort
#AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
#AddIconByType (TXT,/icons/text.gif) text/*
#AddIconByType (IMG,/icons/image2.gif) image/*
#AddIconByType (SND,/icons/sound2.gif) audio/*
#AddIconByType (VID,/icons/movie.gif) video/*
#AddIcon /icons/binary.gif .bin .exe
#AddIcon /icons/binhex.gif .hqx
#AddIcon /icons/tar.gif .tar
#AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
#AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
#AddIcon /icons/a.gif .ps .ai .eps
#AddIcon /icons/layout.gif .html .shtml .htm .pdf
#AddIcon /icons/text.gif .txt
#AddIcon /icons/c.gif .c
#AddIcon /icons/p.gif .pl .py
#AddIcon /icons/f.gif .for
#AddIcon /icons/dvi.gif .dvi
#AddIcon /icons/uuencoded.gif .uu
#AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
#AddIcon /icons/tex.gif .tex
#AddIcon /icons/bomb.gif core
#AddIcon /icons/back.gif ..
#AddIcon /icons/hand.right.gif README
#AddIcon /icons/folder.gif ^^DIRECTORY^^
#AddIcon /icons/blank.gif ^^BLANKICON^^
#DefaultIcon /icons/unknown.gif
AddDescription "GZIP compressed document" .gz
AddDescription "tar archive" .tar
AddDescription "GZIP compressed tar archive" .tgz
ReadmeName README.html
HeaderName HEADER.html
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
AddEncoding x-compress Z
AddEncoding x-gzip gz tgz
AddLanguage ca .ca
AddLanguage cs .cz .cs
AddLanguage da .dk
AddLanguage de .de
AddLanguage el .el
AddLanguage en .en
AddLanguage eo .eo
AddLanguage es .es
AddLanguage et .et
AddLanguage fr .fr
AddLanguage he .he
AddLanguage hr .hr
AddLanguage it .it
AddLanguage ja .ja
AddLanguage ko .ko
AddLanguage ltz .ltz
AddLanguage nl .nl
AddLanguage nn .nn
AddLanguage no .no
AddLanguage pl .po
AddLanguage pt .pt
AddLanguage pt-BR .pt-br
AddLanguage ru .ru
AddLanguage sv .sv
AddLanguage zh-CN .zh-cn
AddLanguage zh-TW .zh-tw
LanguagePriority zh-CN en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-TW
ForceLanguagePriority Prefer Fallback
#AddDefaultCharset ISO-8859-1
AddDefaultCharset GB2312
AddCharset ISO-8859-1 .iso8859-1 .latin1
AddCharset ISO-8859-2 .iso8859-2 .latin2 .cen
AddCharset ISO-8859-3 .iso8859-3 .latin3
AddCharset ISO-8859-4 .iso8859-4 .latin4
AddCharset ISO-8859-5 .iso8859-5 .latin5 .cyr .iso-ru
AddCharset ISO-8859-6 .iso8859-6 .latin6 .arb
AddCharset ISO-8859-7 .iso8859-7 .latin7 .grk
AddCharset ISO-8859-8 .iso8859-8 .latin8 .heb
AddCharset ISO-8859-9 .iso8859-9 .latin9 .trk
AddCharset ISO-2022-JP .iso2022-jp .jis
AddCharset ISO-2022-KR .iso2022-kr .kis
AddCharset ISO-2022-CN .iso2022-cn .cis
AddCharset Big5 .Big5 .big5
# For russian, more than one charset is used (depends on client, mostly):
AddCharset WINDOWS-1251 .cp-1251 .win-1251
AddCharset CP866 .cp866
AddCharset KOI8-r .koi8-r .koi8-ru
AddCharset KOI8-ru .koi8-uk .ua
AddCharset ISO-10646-UCS-2 .ucs2
AddCharset ISO-10646-UCS-4 .ucs4
AddCharset UTF-8 .utf8
AddCharset GB2312 .gb2312 .gb
AddCharset utf-7 .utf7
AddCharset utf-8 .utf8
AddCharset big5 .big5 .b5
AddCharset EUC-TW .euc-tw
AddCharset EUC-JP .euc-jp
AddCharset EUC-KR .euc-kr
AddCharset shift_jis .sjis
#以CGI方式安装PHP
#ScriptAlias "/__php_dir__/" "/apache/cgiphp5/"
#AddType application/x-httpd-php .php4 .php .htm .php3 .inc .fire
#Action application/x-httpd-php "/__php_dir__/php.exe"
#以apache的模块方式运行php
LoadModule php4_module /apache/php/sapi/php4apache.dll
PHPINIDir /apache/php/php.ini
AddType application/x-httpd-php .php .htm .php3
AddType application/x-httpd-php-source .phps
#支持fcgi
#LoadModule fastcgi_module modules/mod_fastcgi.dll
#AddHandler fastcgi-script .fcgi .fpl
AddType application/x-tar .tgz
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddHandler cgi-script .cgi .pl
#AddHandler send-as-is asis
#AddHandler imap-file map
AddHandler type-map var
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /apache/yyfzx/soft
#ErrorDocument 404 "/apache/yyfzx/soft"
#ErrorDocument 402 http://www.example.com/subscription_info.html
BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
BrowserMatch "RealPlayer 4\.0" force-response-1.0
BrowserMatch "Java/1\.0" force-response-1.0
BrowserMatch "JDK/1\.0" force-response-1.0
BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully
BrowserMatch "^WebDrive" redirect-carefully
BrowserMatch "^WebDAVFS/1.[012]" redirect-carefully
BrowserMatch "^gnome-vfs" redirect-carefully
#<Location /server-status>
# SetHandler server-status
# Order deny,allow
# Deny from all
# Allow from .51web.net
#</Location>
#<Location /server-info>
# SetHandler server-info
# Order deny,allow
# Deny from all
# Allow from .51web.net
#</Location>
<IfModule mod_ssl.c>
Include conf/ssl.conf
</IfModule>
#以下是关于虚拟机的配置
NameVirtualHost 218.87.169.209:80
<VirtualHost 218.87.169.209:80>
RewriteEngine On
RewriteRule ^/index.htm$ /index.php
RewriteRule ^/soft/([0-9]+).htm$ /download.php?soft=
RewriteRule ^/sort/([0-9-_]+).htm$ /list.php?list=
RewriteRule ^/sort/index.htm$ /type.php
RewriteRule ^/a-z/([0-9]+).htm$ /az.php?id=
DocumentRoot /apache/mayidown
ServerName mayidown.3322.org
</VirtualHost>
//以 #开头的就是解释用.启动的时候不加载的.我提供了一个虚拟主机给大家参考重写URL功能
什么是盗链
“盗链”的定义是:此内容不在自己服务器上,而通过技术手段,绕过别人放广告有利益的最终页,直接在自己的有广告有利益的页面上向最终用户提供此内容。 常常是一些名不见经传的小网站来盗取一些有实力的大网站的地址(比如一些音乐、图片、软件的下载地址)然后放置在自己的网站中,通过这种方法盗取大网站的空间和流量。
为什么会产生盗链
一般浏览有一个重要的现象就是一个完整的页面并不是一次全部传送到客户端的。如果请求的是一个带有许多图片和其它信息的页面,那么最先的一个Http请求被传送回来的是这个页面的文本,然后通过客户端的浏览器对这段文本的解释执行,发现其中还有图片,那么客户端的浏览器会再发送一条Http请求,当这个请求被处理后那么这个图片文件会被传送到客户端,然后浏览器回将图片安放到页面的正确位置,就这样一个完整的页面也许要经过发送多条Http请求才能够被完整的显示。基于这样的机制,就会产生一个问题,那就是盗链问题:就是一个网站中如果没有起页面中所说的信息,例如图片信息,那么它完全可以将这个图片的连接到别的网站。这样没有任何资源的网站利用了别的网站的资源来展示给浏览者,提高了自己的访问量,而大部分浏览者又不会很容易地发现,这样显然,对于那个被利用了资源的网站是不公平的。一些不良网站为了不增加成本而扩充自己站点内容,经常盗用其他网站的链接。一方面损害了原网站的合法利益,另一方面又加重了服务器的负担。
我遇到的盗链
我的网站遇到最多的是两类盗链,一是图片盗链,二是文件盗链。曾经有一个访问量极大的网站盗链我网站的图片,一天竟然消耗了数G的流量。同时,我站放的不少几十兆的大型软件也常遭到文件盗链,大量消耗我站资源。
盗链的解决方案
其实通过WEB服务器的URL过滤技术,这个伤脑筋的问题会很容易得到解决。
如果WEB服务器用的是APACHE的话,那么使用APACHE自带的Url Rewrite功能可以很轻松地防止各种盗链,其原理是检查REFER,如果REFER的信息来自其他网站则禁止访问所需要的资源。
那么,IIS支持UrlRewrite吗?
答案很简单,不支持。但是我们可以通过安装第三方服务器扩展让IIS支持。
目前有一种产品能比较好地支持IIS的UrlRewrite,名字叫ISAPI_Rewrite。
下载地址在:http://www.helicontech.com/download/
这里只有ISAPI Rewrite的一个LITE版本是免费的,其它都是trial版本。ISAPI Rewrite Lite的版本功能不支持虚拟站点配置,元数据监测和自动缓存清理。 但是基本的UrlRewrite功能都支持。
如何进行UrlRewrite的设置?
isapi_rewrite利用正则表达式进行替换规则的表示。
下面是一个简单的例子,我想让我们的用户输入 http://localhost/test-12314.html 实际上访问的是 http://localhost/test.asp?id=12314 。那么我们的匹配表达式应该是 /test-([0-9]*).html 对应的格式化表达式应该为 /test.asp\?id=$1 。
进行正则表达式的编写的时候,可以利用isapi_rewrite提供的正则表达式测试工具(默认安装提供),进行调试。做好了匹配表达式和格式化表达式,我们可以把它们放到安装目录下的httpd.ini里面。文件保存后,不需重新启动iis即可生效。
对于我的网站,我防盗链的方法是在httpd.ini里面加入如下语句
RewriteCond Host: (.+)
RewriteCond Referer: (?!http://\1.*).*
RewriteRule .*\.(?:gif|jpg|png|exe|rar|zip) /block.gif [I,O]
然后重启IIS,这时防盗链就开始起作用了,其他网站盗链过来的请求都会被拒绝。
至此,我也终于可以摆脱了被盗链的烦恼了。

