nginx指定目录ip访问限制(转)

前言

要实现nginx对指定目录的白名单访问,技术前提

实现重点

  • 正则表达式中()和|的使用,()代表一个原则,|代表或
  • 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_param	HTTPS 	on;
        	include /etc/nginx/fastcgi_params;  
        	fastcgi_pass    php5_fpm;
	}

    	# proxy the PHP scripts to fpm
    	location ~ \.php$ {
		root /srv/; 
		fastcgi_param	HTTPS 	on;
        	include /etc/nginx/fastcgi_params;  
        	fastcgi_pass    php5_fpm;
    	}

转载自:http://blog.csdn.net/wzy_1988/article/details/8516254

猜你喜欢

转载自hz-chenwenbiao-91.iteye.com/blog/1926811