列印

Apache 防盜連說明

本主題由 Linux0911 於 2007-5-12 02:25 PM 移動

Apache 防盜連說明

from: http://www.discuz.net/thread-462466-1-1.html

Apache 防盜連說明

Apache 防盜連說明
最近,大家都比較關心防盜連的問題,擔心安裝了 SupeSite/X-Space 之後,開通博客的朋友多了
他們上傳的圖片被盜連會造成伺服器無謂的負擔,現在我給大家講解一下如何利用 Apache 本身來防盜連 :)

首先,找到您的 apache 設置文件,
一般情況下在 /usr/local/apache/conf/httpd.conf
或者 apache 2.2 的 /usr/local/apache2/conf/extra/httpd-vhost.conf
您可以酌情找到自己的 conf 文件,windows 和 freebsd 下也一樣,然後找到類似如下內容
引用:
這個是帶 rewrite 的

<VirtualHost *:80>
    DocumentRoot /home/www
    ServerName www.yourdomin.com
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/supesite/([0-9]+)/spacelist(.*)$ /supesite/index.php?$1/action_spacelist$2
    RewriteRule ^/supesite/([0-9]+)/viewspace_(.+)$ /supesite/index.php?$1/action_viewspace_itemid_$2
    RewriteRule ^/supesite/([0-9]+)/viewbbs_(.+)$ /supesite/index.php?$1/action_viewbbs_tid_$2
    RewriteRule ^/supesite/([0-9]+)/(.*)$ /supesite/index.php?$1/$2
    RewriteRule ^/supesite/([0-9]+)$ /supesite/index.php?$1
    RewriteRule ^/supesite/action_(.+)$ /supesite/index.php?action_$1
    RewriteRule ^/supesite/category_(.+)$ /supesite/index.php?action_category_catid_$1
    RewriteRule ^/supesite/itemlist_(.+)$ /supesite/index.php?action_itemlist_catid_$1
    RewriteRule ^/supesite/viewnews_(.+)$ /supesite/index.php?action_viewnews_itemid_$1
    RewriteRule ^/supesite/viewthread_(.+)$ /supesite/index.php?action_viewthread_tid_$1
    RewriteRule ^/supesite/index([\.a-zA-Z0-9]*)$ /supesite/index.php
</IfModule>
</VirtualHost>
引用:
這個是不帶 rewrite 的

<VirtualHost *:80>
    DocumentRoot /home/www
    ServerName www.yourdomin.com
</VirtualHost>
在其中加入一段,具體內容如下:
引用:
SetEnvIfNoCase Referer "^http://www.yourdomin.com" local_ref=1
SetEnvIfNoCase Referer "^http://yourdomin.com" local_ref=1
<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
其中紅色的是您的網址,如果有多個,就加多行
綠色的是您需要防盜連的檔案副檔名,中間用 | 隔開

還一種寫法,是用正則的,這種寫法在各個版本的 apache 比較通用。

寫法是
引用:
SetEnvIfNoCase Referer "^http://.*\.yourdomin\.com" local_ref=1
SetEnvIfNoCase Referer ".*\.yourdomin\.com" local_ref=1
<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
其中紅色的部分有一點區別,用正則寫法,  \ 符號代表轉義,因為.本身在正則中有自己的作用。


最終改完就變成了
引用:
<VirtualHost *:80>
    DocumentRoot /home/www
    ServerName www.yourdomin.com
    SetEnvIfNoCase Referer "^http://www.yourdomin.com" local_ref=1
    SetEnvIfNoCase Referer "^http://yourdomin.com" local_ref=1
   <FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
       Order Allow,Deny
       Allow from env=local_ref
   </FilesMatch>
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^/supesite/([0-9]+)/spacelist(.*)$ /supesite/index.php?$1/action_spacelist$2
    RewriteRule ^/supesite/([0-9]+)/viewspace_(.+)$ /supesite/index.php?$1/action_viewspace_itemid_$2
    RewriteRule ^/supesite/([0-9]+)/viewbbs_(.+)$ /supesite/index.php?$1/action_viewbbs_tid_$2
    RewriteRule ^/supesite/([0-9]+)/(.*)$ /supesite/index.php?$1/$2
    RewriteRule ^/supesite/([0-9]+)$ /supesite/index.php?$1
    RewriteRule ^/supesite/action_(.+)$ /supesite/index.php?action_$1
    RewriteRule ^/supesite/category_(.+)$ /supesite/index.php?action_category_catid_$1
    RewriteRule ^/supesite/itemlist_(.+)$ /supesite/index.php?action_itemlist_catid_$1
    RewriteRule ^/supesite/viewnews_(.+)$ /supesite/index.php?action_viewnews_itemid_$1
    RewriteRule ^/supesite/viewthread_(.+)$ /supesite/index.php?action_viewthread_tid_$1
    RewriteRule ^/supesite/index([\.a-zA-Z0-9]*)$ /supesite/index.php
</IfModule>
</VirtualHost>
好了,之後您重新啟動 apache,至此您的盜連命運就結束了,呵呵
祝您辦站生涯愉快!!!
Hack 安裝錯誤, 80% 來自粗心, 請專心安裝
Linux0911 打造您的個性論壇

本人現在忙於研究所, 沒多的時間管論壇, 有問題別發 pm 給我, 謝謝~
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

如要有跟其他論壇交換logo
如何設定顯示呢???
今天發現圖片被盜連
想設定只有logo網址允許連結
麻煩教一下
感謝
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

使用 .htaccess 檔案丟在該附件區限制的資料夾內

修改 png, gif, jpg 等檔案的盜連即可
Hack 安裝錯誤, 80% 來自粗心, 請專心安裝
Linux0911 打造您的個性論壇

本人現在忙於研究所, 沒多的時間管論壇, 有問題別發 pm 給我, 謝謝~
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

引用:
原帖由 Linux0911 於 2007-8-24 08:00 PM 發表
使用 .htaccess 檔案丟在該附件區限制的資料夾內

修改 png, gif, jpg 等檔案的盜連即可
可是我的logo.gif
有在其他站顯示

會不會用了就顯示不出來了
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

.htaccess 丟在要防盜連的資料夾裡面即可

但不建議放在根目錄, 會造成所有圖示失效

如果 logo 所屬的資料夾沒放 .htaccess 就不會在其他站失效的
Hack 安裝錯誤, 80% 來自粗心, 請專心安裝
Linux0911 打造您的個性論壇

本人現在忙於研究所, 沒多的時間管論壇, 有問題別發 pm 給我, 謝謝~
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

複製內容到剪貼簿
代碼:
SetEnvIfNoCase Referer "^http://.*\.yourdomin\.com" local_ref=1
SetEnvIfNoCase Referer ".*\.yourdomin\.com" local_ref=1
<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif)">
Order Allow,Deny
Allow from env=local_ref
</FilesMatch>
只需改這段.
改成.htaccess 丟在要防盜連的資料夾裡面嗎???
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

引用:
SetEnvIfNoCase Referer "^http://.*\.yourdomin\.com" local_ref=1
以上這段請修改為你的網址
引用:
SetEnvIfNoCase Referer ".*\.yourdomin\.com" local_ref=1
以上這段是你如果有子域名網址的時候使用, 沒有就可以刪除

自己測試一下先


在別人的站發表一下文章看看是否防盜連有效
Hack 安裝錯誤, 80% 來自粗心, 請專心安裝
Linux0911 打造您的個性論壇

本人現在忙於研究所, 沒多的時間管論壇, 有問題別發 pm 給我, 謝謝~
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

已經改了
放在logo位子測試
其他論壇顯示可以顯示
附件: 您所在的用戶組無法下載或查看附件
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

請問是放檔名存成 .htaccess 嗎??
那檔名隨意打囉???
改完後放在要防盜連的資料夾理?

論壇的圖片都被複製去了
雖然不是我做的
但也是花時間找來的
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP

回覆 #9 a751115 的帖子

圖如果不是自己製作的, 最好別拿 (有版權的那種)

.htaccess (沒有檔名, 只有副檔名) 不能在 windows 下產生

不過可以使用 cmd 來產生

開始 → 執行 cmd → edit .htaccess

用滑鼠點選 file save as → 右邊是磁碟機跟資料夾目錄

看你要存在哪裡待會找得到

save 完後就去找那的檔案來編輯就好了
Hack 安裝錯誤, 80% 來自粗心, 請專心安裝
Linux0911 打造您的個性論壇

本人現在忙於研究所, 沒多的時間管論壇, 有問題別發 pm 給我, 謝謝~
Linux0911 Forum © 2004-2009 All Rights Reserved.

TOP



論壇聲明
  • 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
    如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
    請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!
最佳瀏覽解析度:不低於 1024 (最佳 1280 以上).(支援 IE 7, Mozilla Firefox, Safari, Chrome)
The Best Surfing Resolution: No Lower than 1024 (Best: above 1280).(IE 7, Mozilla Firefox, Safari, Chrome Supported)