Apache 配置静态缓存+禁止解析+限制访问目录

来源:互联网 发布:学安卓编程 编辑:程序博客网 时间:2024/05/05 12:44

1.Apache 配置静态缓存


为了提高资源的利用率,可以设置文件缓存的时间长短
# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
找到:CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/test.com-access_%Y%m%d_log  86400" combined env=!image-request
在这行下面插入 expires.c静态模块
  <IfModule mod_rewrite.c>
        ExpiresActive on
        ExpiresByType image/gif "access plus 1 days"
        ExpiresByType image/jpeg "access plus 24 hours"
        ExpiresByType image/png "access plus 24 hours"
        ExpiresByType image/css "now plus 2 hour"
        ExpiresByType application/x-javascript "now plus 2 hours"
        ExpiresByType application/x-shockwave-flash "now plus 2 hours"
        ExpiresDefault "now plus 0 min"
</IfModule>

可以设置分钟,小时,天,月的时间单位
"now plus 0 min"------->不进行缓存


/usr/local/apache2/conf/extra/httpd-vhosts.conf -t
/usr/local/apache2/conf/extra/httpd-vhosts.conf graceful 重新加载配置文件


验证:点击图片 右键复制 图片地址
curl -x127.0.0.1:80  'http://www.jqm.com/static/image/common/forum.gif'  -I
    HTTP/1.1 200 OK
    Date: Mon, 08 Aug 2016 12:21:51 GMT
    Server: Apache/2.2.24 (Unix) PHP/5.4.36
    Last-Modified: Tue, 31 May 2016 03:08:36 GMT
    ETag: "80677-257-5341ab0597500"
    Accept-Ranges: bytes
    Content-Length: 599
    Cache-Control: max-age=86400
    Expires: Tue, 09 Aug 2016 12:21:51 GMT
    Content-Type: image/gif


图片的地址操作:
在论坛中右击任意图片,出现选项框后选择复制图像地址{:4_107:}
注意:max-age=86400------>86400为一天,符合设置的1天



2.2.3 Apache配置防盗链

找到:

        ExpiresDefault "now plus 0 min"
</IfModule>   在这后面插入如下:


    SetEnvIfNoCase Referer "^http://.*\.dubingyu\.com" local_ref#白名单,自己的名单需要在白名单里面
     SetEnvIfNoCase Referer ".*\.qzone.qq\.com" local_ref            #白名单,允许用的网

     SetEnvIfNoCase Referer  "^$"  local_ref                                 #做个空白文挡referer
    <filesmatch> "\.(txt|doc|mp3|zip|rar|jpg|gif|png|js|css)">
     Order Allow,Deny
     Allow from env=local_ref
    </filesmatch>

按:wq保存退出,检测语法,重新启动,页面在刷新。。。。。




3.Apache访问控制

[root@yiqiang ~]# vim /usr/local/apache2/conf/httpd.conf  拷贝模板

[root@yiqiang ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf  插入 虚拟配置文件里,没有固定位置
<Directory "/data/www">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
    Deny from 127.0.0.1
</Directory>
按:wq保存退出,apachectl -t  检测,重启,接着curl测试:

[root@yiqiang ~]# curl -x192.168.1.106:80 -I www.dubingyu.com   结果为301
root@yiqiang ~]# curl -x127.0.0.1:80 -I www.dubingyu.com    结果为301
[root@yiqiang ~]# curl -x192.168.1.106:80 -I www.dubingyu.com/forum.php  结果为200 ok.
 [root@yiqiang ~]# curl -x127.0.0.1:80 -I www.dubingyu.com/forum.php  结果为403,即做到了访问限制。ok.

还有个模板,即是访问限制后台。限定制定目录,或网页里面的文件

插入紧接其后:前后都流出空断

<filesmatch "(.*)admin(.*)">    
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1   #白名单
 </filesmatch>


按:wq保存退出,检测语法 Apachectl -t  ,重启restart,刷新页面。

[root@yiqiang ~]# curl -x192.168.1.106:80 -I www.dubingyu.com/admin.php     结果403#只针对 限定的是Admin登陆界面,

[root@yiqiang ~]# curl -x127.0.0.1:80 -I www.dubingyu.com/admin.php      我们想要的结果200,ok。





4.2.5 Apache禁止解析php


[root@yiqiang data]# cd /data/www/data

[root@yiqiang data]# vim info.php

写入如下:

<?php
phpinfo();
?>
按保存退出。 更新apache-t  apachectl restart 

这次实验却是 , 浏览器里面访问www.dubingyu.com/data/info.php 就会直接下载php,
不是我要的结果。
需要做的是禁止这个页面的访问。PHP页面内容 ,禁止观察!!!!
加入以下设置就会禁止访问和解析。


保存退出

<Directory "/data/www/data">
    php_admin_flag engine off
    <filesmatch "(.*)php">
            Order deny,allow
            Deny from all
    </filesmatch>
</Directory>


[root@yiqiang data]# curl -x127.0.0.1:80 www.dubingyu.com/data/info.php

[root@yiqiang data]# curl -x192.168.1.106:80 www.dubingyu.com/data/info.php  结果全是403, Forbidden成了禁止解析。





4.2.6 Apache 禁止指定user_agent

[root@yiqiang logs]# tail  /usr/local/apache2/logs/test.com-access_20170218_log   用tail查看日志里面的访问

[root@yiqiang logs]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf  虚拟机里面

找到mod_rewrite.c模块,直接加入里面

<IfModule mod_rewrite.c>

           #  RewriteCond %{HTTP_USER_AGENT}  ^.*curl.* [NC,OR]           禁带 curl       的访问 【NC不分大小写,OR或着意思】
         RewriteCond %{HTTP_USER_AGENT}  ^.*chrome.* [NC]             禁带 chrome浏览器用来的访问
         RewriteRule .* - [F]                                                                         带上面2种访问的有其一de直接【F】 Forbidden掉.

</IfModule>



[root@yiqiang logs]# curl -x192.168.1.106:80 www.dubingyu.com/forum.php -I     如上配置,结果为状态200,ok

[root@yiqiang logs]# curl -x127.0.0.1:80 www.dubingyu.com/forum.php -I  结果为状态为 403.

[root@yiqiang logs]# curl -A "jkdlsjfdsfhhjkh" -x192.168.1.106:80 www.dubingyu.com/forum.php -I   模拟浏览器curl测试
HTTP/1.1 200 OK  结果为200,

谷歌浏览器已经 Forbidden 403的 加入  chrome测试

#curl -A "jkdlschromejkh"-x192.168.1.106:80 www.dubingyu.com/forum.php -I 

结果为403


禁止指定user_agent 访问成功!




5.2.7 Apache通过rewrite限制某个目录

通过Apache的设定,限制访问某个目录,把上面的4注销掉。


cd /data/www

mkdir tmp

cd tmp/

vim 121212.txt   (touch 1.txt;  echo "hfjksdhfjklsdhjfkl" >121212.txt)

写入东西。。。。

也可以访问,看看效果。


http://www.haoyiyide.com/data/tmp/1122.txt  是可以查看的到的。其状态为200   ok可以查看。

那么


[root@yiqiang logs]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<IfModule mod_rewrite.c>

RewriteCond  %{REQUEST_URI}  ^.*/tmp/.*  [NC]

RewriteRule  .* -  [F]

</IfModule>


:wq保存 退出

测试 下面。

apachectl -t

apachectl restart 

浏览器上访问:

http://www.haoyiyide.com/data/tmp/1122.txt  结果为403 的,表示ok的。







0 0
原创粉丝点击