网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > 服务器环境搭建
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

服务器环境搭建 Windows,Linux,IIS,Apache等服务器配置、安全维护以及PHP和MYSQL运行环境讨论。

Reply
 
Thread Tools Display Modes
  #1   IP: 112.84.222.131
Old 2016-05-31, 06:49 PM
Hammonton Hammonton is offline
初级会员
 
Join Date: 2011-05-03
Posts: 1
Hammonton 现在声名狼藉
Default apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法

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

代码如下:
Code:
<Directory "/var/www/upload">
 <FilesMatch ".PHP">
 Order Allow,Deny
 Deny from all
 </FilesMatch>
 </Directory>
这些配置表面上看起来是没什么问题的,确实在windows下可以这么说。
但是linux就不同了,大家都是知道的linux操作系统是区分大小写的,这里如果换成大写后缀名*.phP一类就pass了

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

Code:
<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文件中
代码如下:

Code:
RewriteEngine on RewriteCond % !^$
 RewriteRule uploads/(.*).(php)$ – [F]
 RewriteRule data/(.*).(php)$ – [F]
 RewriteRule templets/(.*).(php)$ –[F]
另外一种方法,代码如下:
Code:
<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类库:

Code:
<Files ~ "\.inc$">
 Order allow,deny
 Deny from all
 </Files>
禁止访问某些指定的目录:(可以用 <DirectoryMatch> 来进行正则匹配)
Code:
<Directory ~ "^/var/www/(.+/)*[0-9]{3}">
 Order allow,deny
 Deny from all
 </Directory>
通过文件匹配来进行禁止,比如禁止所有针对图片的访问:
Code:
<FilesMatch \.(?i:gif|jpe?g|png)$>
 Order allow,deny
 Deny from all
 </FilesMatch>
针对URL相对路径的禁止访问:
Code:
<Location /dir/>
 Order allow,deny
 Deny from all
 </Location>
针对代理方式禁止对某些目标的访问(<ProxyMatch> 可以用来正则匹配),比如拒绝通过代理访问cnn.com:

Code:
<Proxy http://piaoyun.cc/*>
 Order allow,deny
 Deny from all
 </Proxy>
2. 禁止某些IP访问/只允许某些IP访问
如果要控制禁止某些非法IP访问,在Directory选项控制:
Code:
<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访问,适合比如就允许内部或者合作公司访问:


Code:
<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:解决办法;

Code:
<Directory "/home/domain/public_html">
 Options -Indexes FollowSymLinks
 AllowOverride All
 <Files ~ ".txt">
 Order allow,deny
 Deny from all
 </Files>
 </Directory>
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 07:52 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.