一、配置防盜鏈:免費網絡或者論壇,有時候若是網站流量偏高,抓包發現是圖片的流量居多,發現是從某個站點鏈接跳過來的,原來是咱們的網站,被用戶上傳了許多色情圖片,而用戶又在他本身的網站上加了咱們網站圖片的連接,就能本身訪問鏈接跳轉到咱們這個,致使流量很高:php
配置了防盜鏈後的做用:咱們本身網站的圖片,只能本身來訪問,其餘借用是不能夠的:html
1:配置防盜鏈: /usr/local/apapche.24/conf/extra/http-vhosts.confweb
須要增長的配置文件內容以下:apache
Directory針對目錄進行 <Directory /data/wwwroot/111.com> #用於定義做用於哪一個目錄,配置防盜鏈,就是定義本站點 SetEnvIfNoCase Referer 「http://www.111.com」 local_ref #定義Referer白名單 SetEnvIfNoCase Referer 「http://aaa.com」 local_ref #定義白名單 SetEnvIfNoCase Referer 「^$」 local_ref #定義空的Referer頁面爲白名單 <filesmatch 「.(txt|doc|mp3|zip|rar|jpg|gif|png)」> #定義規則,這裏的filesmatch中f 和 m 能夠大寫能夠小寫 Order Allow,Deny #定義訪問控制,order用來定義訪問順序 Allow from env=local_ref </filesmatch> </Directory>
更改前的配置文件:vim
[root@localhost_002 conf]# vim extra/httpd-vhosts.conf # Virtual Hosts <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin yuanhh@foreb.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias www.example.com www.2111.com #<Directory /data/wwwroot/111.com> #<FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </FilesMatch> # </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.111.com$ RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" combined </VirtualHost>
添加防盜鏈後的配置文件:註釋:將Referer爲空的白名單註釋掉: #SetEnvIfNoCase Referer 「^$」 local_ref 瀏覽器
[root@localhost_002 extra]# cat httpd-vhosts.conf # Virtual Hosts <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin yuanhh@foreb.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias www.example.com www.2111.com #<Directory /data/wwwroot/111.com> #<FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # </FilesMatch> # </Directory> # Directory針對目錄進行 <Directory /data/wwwroot/111.com> SetEnvIfNoCase Referer "http://www.111.com" local_ref SetEnvIfNoCase Referer "http://www.example.com" local_ref # SetEnvIfNoCase Referer "^$" local_ref <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)"> Order Allow,Deny Allow from env=local_ref </filesmatch> </Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.111.com$ RewriteRule ^/(.*)$ http://www.111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" combined </VirtualHost>
註釋:刷新的時候就是空Referer了,或者直接複製圖片網址來打開時也是空Referer,當用第三方站點過來的時候也是空Referer了,要想訪問須要打開Referer就能夠了:bash
三、檢測文件是否有錯誤:網絡
[root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl -t Syntax OK [root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl graceful
四、而後訪問:www.111.com/image/kaola.jpgcurl
註釋:若是這時候在論壇建立一個帖子,將鏈接放進帖子中,再去訪問,會看到仍是Forbidden,這是由於Referer時論壇的網址:ide
若想訪問,則須要加入到白名單中:
註釋:如果想要直接就能夠在瀏覽器中訪問,則只須要去虛擬主機配置文件中打開空的Referer:
SetEnvIfNoCase Referer "^$" local_ref
檢測並重啓服務:
[root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl -t Syntax OK [root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl graceful
五、也能夠用curl命令去測試:
[root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl graceful [root@localhost_002 extra]# curl -x127.0.0.1:80 www.111.com/image/kaola.jpg -I HTTP/1.1 200 OK Date: Sun, 07 Oct 2018 15:16:10 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Last-Modified: Tue, 14 Jul 2009 05:32:31 GMT ETag: "bea1f-46ea3c3d3b9c0" Accept-Ranges: bytes Content-Length: 780831 Content-Type: image/jpeg
也能夠自定義Referer: curl -e "http://www.qq.com/123.txt" -x127.0.0.1:80
[root@localhost_002 extra]# curl -e "http://www.qq.com/123.txt" -x127.0.0.1:80 www.111.com/image/kaola.jpg -I HTTP/1.1 403 Forbidden Date: Sun, 07 Oct 2018 15:17:36 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1
六、訪問控制: Order deny,allow (管IP是否匹配到,它都會從頭至尾執行完)
首先打開虛擬主機配置:
Order deny,allow 用來定義順序,是先deny,仍是allow:
如果先deny,就先執行deny的語句:
如果先allow,就先自行allow的語句:
[root@localhost_002 extra]# vim /usr/local/apapche2.4/conf/extra/httpd-vhosts.conf <Directory /data/wwwroot/111.com/admin/> Order deny,allow Deny from all Allow from 127.0.0.1 </Directory>
2:在根目錄/data/wwwroot/www.111.com/下建立一個admin目錄,並新建一個index.php的文件,內容爲12121.
[root@localhost_002 extra]# cd /data/wwwroot/ [root@localhost_002 wwwroot]# ls 111.com abc.com [root@localhost_002 wwwroot]# cd 111.com/ [root@localhost_002 111.com]# mkdir admin [root@localhost_002 111.com]# cd admin [root@localhost_002 admin]# vim index.php erqwe231423421
3:檢測語法,並從新加載配置文件:
[root@localhost_002 admin]# /usr/local/apapche2.4/bin/apachectl -t Syntax OK [root@localhost_002 admin]# /usr/local/apapche2.4/bin/apachectl graceful
4:測試:因爲在上述命令中禁止了全部源IP訪問,只容許127.0.0.1訪問,最終就是目標IP和原IP是同一個IP,本身和本身通訊,限制IP實際上是源IP:
curl -x127.0.0.1:80 www.111.com/admin/index.php -I 註釋: -x 指定目標IP:
[root@localhost_002 extra]# curl -x127.0.0.1:80 www.111.com/admin/index.php -I HTTP/1.1 200 OK Date: Sun, 07 Oct 2018 15:30:32 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Content-Type: text/html; charset=UTF-8
在更換目標IP後,源IP也會跟着變化:
[root@localhost_002 extra]# curl -x192.168.149.130 www.111.com/admin/index.php -I curl: (7) Failed connect to 192.168.149.130:1080; 拒絕鏈接 [root@localhost_002 extra]# curl -x192.168.149.130:80 www.111.com/admin/index.php -I HTTP/1.1 403 Forbidden Date: Sun, 07 Oct 2018 15:38:26 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1
網頁訪問也沒法訪問 www.111.com/admin/inde.php
註釋:訪問控制是用目錄的形式來作的,首先規定一個目錄訪問到哪裏去的(目錄必須使用絕對路徑),而後是Oerder,控制的對象就是來源IP:
註釋:默認不定義deny規則,也會deny,若是想設置只容許幾個IP訪問的話,不定義deny的話,默認也是deny掉了,能夠經過以下定義:
Order allow deny
allow from 127.0.0.1
七、訪問控制: <FileMatch> 訪問控制,除了目錄形式,仍是文件名去匹配(或連接)
打開虛擬配置文件:
首先定義一個Directory,在下面定義一個FileMatch:
<Directory /data/wwwroot/111.com> <FilesMatch admin.php(.*)> Order deny,allow Deny from all Allow from 127.0.0.1 </FilesMatch> </Directory>
檢測語法錯誤,並重啓apache服務:
[root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl -t Syntax OK [root@localhost_002 extra]# /usr/local/apapche2.4/bin/apachectl graceful
測試: curl -x192.168.149.130:80 www.111.com/admin/asdfsdfsdf -I
[root@localhost_002 extra]# curl -x192.168.149.130:80 www.111.com/admin/asdfsdfsadf -I HTTP/1.1 404 Not Found Date: Sun, 07 Oct 2018 16:33:05 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1
註釋:這裏沒有對admin目錄作任何限制,作的FilesMatch跟這個不匹配,沒有遇到控制語句,因此訪問是404
有特殊符號須要用 」 單引號括起來:
[root@localhost_002 extra]# curl -x192.168.149.130:80 'http://www.111.com/admin.phpasdfsdfsadf' -I HTTP/1.1 403 Forbidden Date: Sun, 07 Oct 2018 16:35:47 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 [root@localhost_002 extra]# curl -x127.0.0.1:80 'http://www.111.com/admin.phpasdfsdfsadf' -I HTTP/1.1 404 Not Found Date: Sun, 07 Oct 2018 16:36:45 GMT Server: Apache/2.4.34 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1
註釋:Directory和FilesMatch 功能是同樣的,只不過有時僅僅想針對一個訪問的連接去作控制,那麼再去控制目錄則不太合適,FilesMatch能夠知足一些比較個性化的需求:
擴展 幾種限制ip的方法 http://ask.apelearn.com/question/6519 apache 自定義header http://ask.apelearn.com/question/830 apache的keepalive和keepalivetimeout http://ask.apelearn.com/question/556