nginx指定目录ip访问限制

来源:互联网 发布:知我无情有情 张南雁 编辑:程序博客网 时间:2024/05/21 06:31

前言

要实现nginx对指定目录的白名单访问,技术前提
  • 正则表达式应用(自己学习即可)
  • nginx的location规则匹配,参考链接:http://blog.csdn.net/zinss26914/article/details/8182625
  • nginx的http access模块应用,参考链接:http://blog.csdn.net/zinss26914/article/details/8185336

实现重点

  • 正则表达式中()和|的使用,()代表一个原则,|代表或
  • nginx的location匹配规则中,有一条按照文件顺序进行正则匹配(ps:可以把需要匹配的目录放置在server模块开始的位置)
  • allow和deny的使用

示例

目录结构

根目录/srv/
test1 /  --   hello.php
test2/   --   hello.php
test3/   --   hello.php
test4/   --   {hello.php,1.php,2.php}

访问需求

对于test1,test2目录,只允许指定的192.168.1.101ip地址访问,禁止其它ip访问
对于其他目录的php程序,所有ip地址均可以访问

实现的nginx配置文件

#指定目录实行白名单访问机制location ~ ^/(test1|test2)/ {allow 192.168.1.101;deny all;root /srv/; fastcgi_paramHTTPS on;        include /etc/nginx/fastcgi_params;          fastcgi_pass    php5_fpm;}    # proxy the PHP scripts to fpm    location ~ \.php$ {root /srv/; fastcgi_paramHTTPS on;        include /etc/nginx/fastcgi_params;          fastcgi_pass    php5_fpm;    }




原创粉丝点击