nginx怎么设置指定目录ip访问限制?方法教程
时间:2014-11-23 13:30 来源:www.it.net.cn 作者:admin
nginx对指定目录设置ip访问限制的方法
nginx对指定目录的白名单访问,需要掌握:
正则表达式用法,nginx的location规则匹配,以及nginx的http access模块用法。
正则表达式中()和|的使用,()代表一个原则,|代表或
nginx的location匹配规则中,有一条按照文件顺序进行正则匹配(ps:可以把需要匹配的目录放置在server模块开始的位置)
allow和deny的使用
通过实例学习下nginx指定目录ip访问限制的方法,见如下教程。
1、目录结构:
根目录/srv/
test1 / -- hello.php
test2/ -- hello.php
test3/ -- hello.php
test4/ -- {hello.php,1.php,2.php}
2、访问需求
1)、对于test1,test2目录,只允许指定的192.168.1.101ip地址访问,禁止其它ip访问。
2)、对于其他目录的php程序,所有ip地址均可以访问。
3、nginx配置文件
#指定目录实行白名单访问机制
location ~ ^/(test1|test2)/ {
allow 192.168.1.101;
deny all;
root /srv/;
fastcgi_param HTTPS on;
include /etc/nginx/fastcgi_params;
fastcgi_passphp5_fpm;
}
# proxy the PHP scripts to fpm
location ~ \.php$ {
root /srv/;
fastcgi_param HTTPS on;
include /etc/nginx/fastcgi_params;
fastcgi_passphp5_fpm;
}
(责任编辑:IT)
nginx对指定目录设置ip访问限制的方法
nginx对指定目录的白名单访问,需要掌握:
正则表达式中()和|的使用,()代表一个原则,|代表或 通过实例学习下nginx指定目录ip访问限制的方法,见如下教程。
1、目录结构:
根目录/srv/
test1 / -- hello.php test2/ -- hello.php test3/ -- hello.php test4/ -- {hello.php,1.php,2.php}
2、访问需求
3、nginx配置文件
#指定目录实行白名单访问机制
location ~ ^/(test1|test2)/ { allow 192.168.1.101; deny all; root /srv/; fastcgi_param HTTPS on; include /etc/nginx/fastcgi_params; fastcgi_passphp5_fpm; } # proxy the PHP scripts to fpm location ~ \.php$ { root /srv/; fastcgi_param HTTPS on; include /etc/nginx/fastcgi_params; fastcgi_passphp5_fpm; } |