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.youkuaiyun.com/wzy_1988/article/details/8516254

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值