免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2975 | 回复: 0
打印 上一主题 下一主题

nginx下目录限制ip访问的解决办法 [复制链接]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-04 10:53 |只看该作者 |倒序浏览

apache
下限制某个目录访问很容易的,
nginx
下的方法也差不多,只不过大家用的少而已,我简单的总结一下:
根据nginx的文档:
QUOTE:
ngx_http_access_module
This module provides a simple host-based access control.
Module ngx_http_access_module makes it possible to control access for specific IP-addresses of clients. Rules are checked in the order of their record to the first match.
Example configuration
location
/ {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
deny all;
}
In the above example access is only granted to networks 10.1.1.0/16 and 192.168.1.0/24 with the exception of the address 192.168.1.1.
When implementing many rules, it is generally better to use the ngx_http_geo_module.根据这个文档,比如我要限制private这个目录的访问,用如下规则
location /private{
allow 192.168.1.0/24;
deny all;
}
location ~ \.php$
{
include conf/enable_php5.conf;
}
这个时候实验会发现,private目录下的
php
之外的
文件
确实只有192.168.1.0这个网段的机器访问,但是php文件却依然可以访问,这是为什么哪?
因为nginx的匹配方式是
正则表达式
优先级比较高。因此PHP解析用的是正则表达式进行匹配,而要限制的目录如果不是用正则表达式,所以,就算是要限制的目录,因为PHP还是能被匹配到,所以,还是解析PHP了。
所以,如果想解决的话,需要把目录也写成正则匹配,而且要放在PHP的前面,否则就会先匹配PHP。
location ~ ^/private/ {
allow 192.168.1.0/24;
deny all;
}
location ~ \.php$
{
include conf/enable_php5.conf;
}
改成这样以后,会发现php文件提示打开、保存,我点了保存以后,
下载
回来的文件就是明文的
源代码
。这又是为什么哪?
根据nginx的文档:
QUOTE:
location
syntax: location [=|~|~*|^~] /uri/ { ... }
default: no
context: server
This directive allows different configurations depending on the URI. It can be configured using both conventional strings and regular expressions. To use regular expressions, you must use the prefix ~* for case insensitive match and ~ for case sensitive match.
To determine which location directive matches a particular query, the conventional strings are checked first. Conventional strings match the beginning portion of the query and are case-sensitive - the most specific match will be used (see below on how nginx determines this). Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the convention string search is used.在location中使用正则表达式去匹配的话,第一个匹配上的就不会再去匹配别的规则了,因此下面的那个匹配php文件的规则实际上被忽略了,因此php文件访问的时候就提示是打开还是保存了。
因此解决办法就是:单独把private目录下的php文件限制也写到规则里面,而且在php文件解析的规则之前:
location /private/ {
    allow 192.168.1.0/24;
    deny all;
}
location ~ ^/private/.*\.php$
{
    allow 192.168.1.0/24;
    deny all;
    include conf/enable_php5.conf;
}
location ~ \.php$
{
    include conf/enable_php5.conf;
}
这样就可以
实现
我们的要求了,private目录下的文件都严格按照ip限制来访问,php文件也可以解析。


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4206/showart_1359070.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP