访问控制
以下针对apache虚拟主机配置
网站访问,限制指定ip才可以访问。只允许内网登录,不允许其他地址登录。
Order定义顺序,先deny,然后执行下面的deny from all,然后执行allow,再执行allow from 127.0.0.1,意思是拒绝所有的,只允许本地可以访问。
<Directory /data/www/>
<filesmatch ".*">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</filesmatch>
</Directory>
针对请求的url去限制,只允许内网和指定ip可以访问包含admin关键词的地址,一般为管理后台;
<filesmatch "(.*)admin(.*)">
Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from 2.2.2.2
假如该虚拟机的域名为 domain.com , 这样配置后,除了 127.0.0.1 和 2.2.2.2 外,其他ip访问以下类似的uri时都会直接禁止的。
http://domain.com/1212admin.txt
http://domain.com/admin.php
http://domain.com/1212/admin.html 等
<a href="http://s3.51cto.com/wyfs02/M00/6C/81/wKioL1VLBGSjzkBUAAEsiK1h_ck263.jpg" target="_blank"></a>
某个目录下禁止解析php,目录可以自定义;php解析失败的话会显示源代码,对于网站来说不安全。
<Directory /data/www/>
php_admin_flag engine off
<filesmatch "(.*)php">
Order deny,allow
Deny from all
</filesmatch>
1
2
3
4
5
<code>[root@localhost www]</code><code># curl -x127.0.0.1:80 www.111.com/forum.php -I</code>
<code>HTTP</code><code>/1</code><code>.1 403 Forbidden</code>
<code>Date: Mon, 04 May 2015 07:41:15 GMT</code>
<code>Server: Apache</code><code>/2</code><code>.2.29 (Unix) DAV</code><code>/2</code> <code>PHP</code><code>/5</code><code>.3.28</code>
<code>Content-Type: text</code><code>/html</code><code>; charset=iso-8859-1</code>
<a href="http://s3.51cto.com/wyfs02/M01/6C/85/wKiom1VLA1jzVaxuAADrjMrlOFQ601.jpg" target="_blank"></a>
apache rewrite模块应用——伪静态网页
mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面。
伪静态页面,网址域名后面的地址规范填写,看起来更美观,适合SEO搜索。
discuz! 管理后台页面——全局——左侧的SEO设置——URL伪静态,右边的可用选项全部打勾。
<a href="http://s3.51cto.com/wyfs02/M01/6C/85/wKiom1VLBW-xhZmUAAVXXQzkW5I298.jpg" target="_blank"></a>
discuz伪静态配置,写入到虚拟主机配置rewrite模块中;forum是版块,thread是帖子;
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/topic-(.+)\.html$ /portal.php?mod=topic&topic=$1&%1
RewriteRule ^/article-([0-9]+)-([0-9]+)\.html$ /portal.php?mod=view&aid=$1&page=$2&%1
RewriteRule ^/forum-(\w+)-([0-9]+)\.html$ /forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteRule ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteRule ^/group-([0-9]+)-([0-9]+)\.html$ /forum.php?mod=group&fid=$1&page=$2&%1
RewriteRule ^/space-(username|uid)-(.+)\.html$ /home.php?mod=space&$1=$2&%1
RewriteRule ^/blog-([0-9]+)-([0-9]+)\.html$ /home.php?mod=space&uid=$1&do=blog&id=$2&%1
RewriteRule ^/archiver/(fid|tid)-([0-9]+)\.html$ /archiver/index.php?action=$1&value=$2&%1
RewriteRule ^/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ /plugin.php?id=$1:$2&%1
</IfModule>
实验结果:
帖子地址为:http://www.111.com/forum.php?mod=viewthread&tid=1&extra=
伪静态之后为:http://www.111.com/thread-1-1-1.html
apache 限制指定user_agent
有些user_agent 不是我们想要的,可以通过rewrite功能针对 %{HTTP_USER_AGENT} 来rewirete到403页,从而达到限制某些user_agent的请求。
apache的rewrite功能有一项就是forbidden,那就是 F,重写规则会显示403错误,无法打开网页。
限制curl和IE浏览器8.0不能访问,360的浏览器版本为IE 7.0可以访问。USER_AGENT具体的版本需要看日志的详细信息,然后进行限制。
例如:("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)")
如禁用所有的IE浏览器:RewriteCond %{HTTP_USER_AGENT} ^.*MSIE*
<IFModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^.*MSIE\ 8.0* [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^.*curl* [NC]
RewriteRule .* - [F]
</IFModule>
测试结果,使用curl本地解析网站为403错误;网页访问IE提示403错误,360浏览器可以打开。
<code>[root@localhost www]</code><code># curl -x127.0.0.1:80 www.1111.com -I</code>
<code>Date: Thu, 07 May 2015 02:09:58 GMT</code>
<a href="http://s3.51cto.com/wyfs02/M02/6C/85/wKiom1VLBCrhw4TsAAG9s6IZRMk362.jpg" target="_blank"></a>
本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1643838,如需转载请自行联系原作者