• apache配置禁止访问某些文件/目录


     我们来看俩段通常对上传目录设置无权限的列子,配置如下:

    代码如下:

    1
    2
    3
    4
    5
    6
    <Directory "/var/www/upload">
    <FilesMatch ".php">
    Order Allow,Deny
    Deny from all
    </FilesMatch>
    </Directory>

    这些配置表面上看起来是没什么问题的,确实在windows下可以这么说。
    但是linux就不同了,大家都是知道的linux操作系统是区分大小写的,这里如果换成大写后缀名*.phP一类就pass了

    这里我说下我个人的解决方法,代码如下:

    <Directory "要去掉PHP执行权限的目录路径,例如:D:/piaoyun.cc/upload">
    ErrorDocument 404 /404/404.html
    ErrorDocument 403 /404/403.html
    <FilesMatch ".(?i:php|php3|php4)$"> // ?是尽可能多的匹配.php的字符串,i是不区分大小写,然后冒号后面跟上正则表达式,也可以写成:<FilesMatch ".(php|php3)$">
    Order allow,deny
    Deny from all
    </FilesMatch>
    </Directory>

    上面的意思就是说,<Directory "要去掉PHP执行权限的目录路径,例如:D:/piaoyun.cc/upload"> 内目录路径下所有php文件不区分大小写,通过order,allow,deny原则判断拒绝执行php文件,对nginx同样也是可应用的

    另外一种方法,是设置在htaccess里面的,这个方法比较灵活一点,针对那些没有apapche安全操作权限的网站管理员:
    Apache环境规则内容如下:Apache执行php脚本限制 把这些规则添加到.htaccess文件中
    代码如下:

    1
    2
    3
    4
    RewriteEngine on RewriteCond % !^$
    RewriteRule uploads/(.*).(php)$ – [F]
    RewriteRule data/(.*).(php)$ – [F]
    RewriteRule templets/(.*).(php)$ –[F]

    另外一种方法,代码如下:

    <Directory "/var/www/upload">
    php_admin_flag engine off
    </Directory>

    此方法我在win系统下面测试失败了,重新启动apapche出现下面的错误信息:
    The Apache service named reported the following error:
    >>> Invalid command 'php_flag', perhaps misspelled or defined by a module not included in the server configuration.

    这里我就不具体说明这个解决办法了,因为禁止php执行的方法,大家看自己的需求去设置就可以了!

    【apache配置禁止访问】
    1. 禁止访问某些文件/目录
    增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库:
    <Files ~ ".inc$">
       Order allow,deny
       Deny from all
    </Files>

    多个的话可以这样配置已验证过:

    #不允许访问 .inc .txt .sql .conf扩展名的文件
    <Files ~ ".(inc|txt|sql|conf)$">
    Order allow,deny
    Deny from all
    </Files>



    禁止访问某些指定的目录:(可以用 <DirectoryMatch>   来进行正则匹配)

    <Directory ~ "^/var/www/(.+/)*[0-9]{3}"> 
       Order allow,deny
       Deny from all
    </Directory>

    通过文件匹配来进行禁止,比如禁止所有针对图片的访问:
    <FilesMatch .(?i:gif|jpe?g|png)$>
       Order allow,deny
       Deny from all
    </FilesMatch> 

    针对URL相对路径的禁止访问:
    <Location /dir/>
       Order allow,deny
       Deny from all
    </Location> 

    针对代理方式禁止对某些目标的访问(<ProxyMatch> 可以用来正则匹配),比如拒绝通过代理访问cnn.com:
    <Proxy http://cnn.com/*>
       Order allow,deny
       Deny from all
    </Proxy> 

    2. 禁止某些IP访问/只允许某些IP访问 
    如果要控制禁止某些非法IP访问,在Directory选项控制:
    <Directory "/var/www/web/">
       Order allow,deny
       Allow from all
       Deny from 10.0.0.1 #阻止一个IP
       Deny from 192.168.0.0/24 #阻止一个IP段
    </Directory>

    只允许某些IP访问,适合比如就允许内部或者合作公司访问:
    <Directory "/var/www/web/">
       Order deny,allow
       Deny from all
       All from example.com #允许某个域名
       All from 10.0.0.1 #允许一个iP
       All from 10.0.0.1 10.0.0.2 #允许多个iP
       Allow from 10.1.0.0/255.255.0.0 #允许一个IP段,掩码对
       All from 10.0.1 192.168 #允许一个IP段,后面不填写
       All from 192.168.0.0/24 #允许一个IP段,网络号
    </Directory>


    Apache:解决办法;
    <Directory "/home/domain/public_html">
    Options -Indexes FollowSymLinks
    AllowOverride All
    <Files ~ ".txt">
    Order allow,deny
    Deny from all
    </Files>
    </Directory>

  • 相关阅读:
    个推技术实践 | 掌握这两个调优技巧,让你的TiDB性能提速千倍!
    3·8节,带你走进程序媛小姐姐的一天.mp4
    c++ fstream读写方式
    c++ ignore
    AI 智能写情诗、藏头诗
    随机数生成
    R script 入门教程
    Error: Cannot find module '@vue/clipluginbabel'
    Please, upgrade your dependencies to the actual version of corejs.
    java获取不到真实ip;获取不到ip地址;
  • 原文地址:https://www.cnblogs.com/as3lib/p/6764260.html
Copyright © 2020-2023  润新知