Apache(httpd)配置--防盗链配置和访问控制
生活随笔
收集整理的這篇文章主要介紹了
Apache(httpd)配置--防盗链配置和访问控制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、配置防盜鏈
通過防盜鏈的方式,可以設置限制第三方的站點通過引用的方式獲取服務器上的圖片,數據等,如果想要獲取本站點的圖片數據,只能通過本站點訪問獲取,這樣也有效的減少了服務器的資源。
什么是referer?
referer是http數據包的header的一部分,當瀏覽器其向服務器發送請求時,將帶上referer,以此來告訴瀏覽器該請求時從什么網頁鏈接過來的,瀏覽器處理該鏈接并顯示。
比如:在A網站的某個也頁面http://aaa.com/a.html里面的鏈接去訪問B站的某個頁面http://bbb.com/b.html,那么B網站的reffer就是http://aaa.com/a.html
步驟1:修改虛擬主機配置文件
[root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf <Directory /data/wwwroot/123test> # 把linuxtest.com設為白名單,對應規則AllowSetEnvIfNoCase Referer "http://linuxtest.com" local_ref # 把某個ip設為白名單,對應規則AllowSetEnvIfNoCase Referer "http:http://120.78.56.104/" local_ref # 把空referer設為白名單,對應規則Allow;空referer即直接訪問的地址SetEnvIfNoCase Referer "^$" local_ref # 對txt、doc等格式的文件執行訪問控制<FilesMatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)"> # 白名單地址allow,其他deny # 執行順序依次為allow、deny,反過來將導致都被禁止訪問Order Allow,Deny # 白名單為local_ref對應的地址Allow from env=local_ref</FilesMatch></Directory>[root@zlinux ~]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful二、訪問控制(Directory)
限制用戶訪問部分目錄,允許特定ip訪問
步驟1:修改虛擬主機配置文件
[root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //添加以下內容<Directory /data/wwwroot/123test/admin/>Order deny,allow # 設定Deny和Allow的先后順序Deny from allAllow from 127.0.0.1 # 只允許本地訪問</Directory>[root@zlinux ~]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful步驟2:測試是否生效
[root@zlinux ~]# curl -x 127.0.0.1:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I HTTP/1.1 200 OK Date: Tue, 06 Mar 2018 09:40:48 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Cache-Control: max-age=0 Expires: Tue, 06 Mar 2018 09:40:48 GMT Content-Type: text/html; charset=UTF-8 //來源IP:127.0.0.1訪問成功[root@zlinux ~]# curl -x 192.168.204.128.1:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I curl: (5) Could not resolve proxy: 192.168.204.128.1; 未知的名稱或服務 [root@zlinux ~]# curl -x 192.168.204.128:80 -e "http://linuxtest.com" linuxtest.com/admin/admintest.php -I HTTP/1.1 403 Forbidden Date: Tue, 06 Mar 2018 09:41:41 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 //非來源IP則被阻止訪問三、訪問控制(FilesMatch)
限制指定文件的訪問
步驟1:修改配置文件
[root@zlinux ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //加入以下內容<Directory /data/wwwroot/123test>//對文件admin.php進行限制<FilesMatch "admin.php(.*)"> Order deny,allowDeny from allAllow from 127.0.0.1</FilesMatch> </Directory>[root@zlinux ~]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@zlinux ~]# /usr/local/apache2/bin/apachectl graceful步驟2:測試
[root@zlinux ~]# curl -x 127.0.0.1:80 http://linuxtest.com/admin/admintest.php?123 -I HTTP/1.1 404 Not Found Date: Tue, 21 Nov 2017 15:12:34 GMT Server: Apache/2.4.28 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1//說明:使用允許的ip訪問,由于文件不存在,返回404狀態碼四、訪問控制(user_agent)
user_agent(用戶代理):是指瀏覽器(搜索引擎)的信息包括硬件平臺、系統軟件、應用軟件和用戶個人偏好。
有時候網站受到CC***,其原理是:***者借助代理服務器(肉機)生成指向受害主機的合法請求,實現DDOS和偽裝。CC***的一個特點就是其useragent是一致的,所以,可以通過限制***者useragent的方法來阻斷其***。
步驟1:修改配置文件
[root@zlinux 123test]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf<IfModule mod_rewrite.c>RewriteEngine onRewriteCond %{HTTP_USER_AGENT} .*curl.* [NC,OR]RewriteCond %{HTTP_USER_AGENT} .*baidu.com.* [NC]RewriteRule .* - [F] # NC表示忽略大小寫,OR選項表示或者(不加任何選項表并且)連接下一個條件,F=forbidden禁止。</IfModule>[root@zlinux 123test]# /usr/local/apache2/bin/apachectl -t Syntax OK [root@zlinux 123test]# /usr/local/apache2/bin/apachectl graceful步驟2:測試
[root@zlinux 123test]# curl -x 192.168.204.128:80 linuxtest.com/ -I HTTP/1.1 403 Forbidden Date: Wed, 07 Mar 2018 07:01:54 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1[root@zlinux 123test]# curl -A "www.baidu.com" -x 192.168.204.128:80 linuxtest.com/ -I HTTP/1.1 403 Forbidden Date: Wed, 07 Mar 2018 07:02:20 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 Content-Type: text/html; charset=iso-8859-1 //-A指定user_agent [root@zlinux 123test]# curl -A "www" -x 192.168.204.128:80 linuxtest.com/ -I HTTP/1.1 200 OK Date: Wed, 07 Mar 2018 07:02:29 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Cache-Control: max-age=0 Expires: Wed, 07 Mar 2018 07:02:29 GMT Content-Type: text/html; charset=UTF-8第一個和第二個分別匹配對應條件,所以狀態碼是403。
轉載于:https://blog.51cto.com/3069201/2083552
總結
以上是生活随笔為你收集整理的Apache(httpd)配置--防盗链配置和访问控制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET 环境中使用RabbitMQ
- 下一篇: Orange Business Serv