zoukankan      html  css  js  c++  java
  • Apache和Nginx下禁止访问特定的目录或文件

    大家是否测试Apache做了目录禁止浏览后,目录下面的txt文件还是可以显示里面的内容的。
    例如:http://www.domain.com/test/此访问会报403错误,但是如果test下有很多txt,你访问该txt时;
    例如:http://www.domain.com/test/a.txt,此时a.txt里的内容会全部暴露在外面了(有时这个txt是很机密的文件),这样以来就不安全了。
    同样:我在Nginx配置后后也存在这样的问题,Apache下此问题的解决多谢NetSeek帮助。
    如下是关于Apache和Nginx 限制该类事情办法:

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

    Nginx:解决办法;
    location ~* .(txt|doc)$ {
    if (-f $request_filename) {
    root /home/domain/public_html/test;
    break;

    }

    }


    Nginx下请大家注意标点符号的使用,不要漏掉后面的“;”!

  • 相关阅读:
    [LeetCode] 44. Wildcard Matching
    [LeetCode] 1431. Kids With the Greatest Number of Candies
    [LeetCode] 47. Permutations II
    [LeetCode] 77. Combinations
    [LeetCode] 40. Combination Sum II
    [LeetCode] 39. Combination Sum
    [LeetCode] 213. House Robber II
    [LeetCode] 198. House Robber
    [LeetCode] 338. Counting Bits
    [LeetCode] 259. 3Sum Smaller
  • 原文地址:https://www.cnblogs.com/sdgwc/p/3397394.html
Copyright © 2011-2022 走看看