单看nginx模块名ngx_http_access_module
,很多人一定很陌生,但是deny和allow相比没一个人不知道的,实际上deny和allow指令属于ngx_http_access_module.我们想控制某个uri或者一个路径不让人访问,在nginx就得靠它了。
nginx的访问控制模块语法很简单,至少比apache好理解,apache的allow和deny的顺序让很多初学者抓头.好了具体看下这个插件的使用方法吧。文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
1、安装模块
这个模块内置在了nginx中,除非你安装中使用了--without-http_access_module。如果你还没安装过nginx,那么请参考下ttlsa之前写的nginx安装.文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
2、指令
allow
语法: allow address | CIDR | unix: | all;
默认值: —
配置段: http, server, location, limit_except文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
允许某个ip或者一个ip段访问.如果指定unix:,那将允许socket的访问.注意:unix在1.5.1中新加入的功能,如果你的版本比这个低,请不要使用这个方法。文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
deny
语法: deny address | CIDR | unix: | all;
默认值: —
配置段: http, server, location, limit_except文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
禁止某个ip或者一个ip段访问.如果指定unix:,那将禁止socket的访问.注意:unix在1.5.1中新加入的功能,如果你的版本比这个低,请不要使用这个方法。文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
3. allow、deny实例
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
从上到下的顺序,类似iptables。匹配到了便跳出。如上的例子先禁止了192.16.1.1,接下来允许了3个网段,其中包含了一个ipv6,最后未匹配的IP全部禁止访问. 在实际生产环境中,我们也会使用nginx 的geo模块配合使用,有兴趣的请参考ttlsa相关文章nginx geo使用方法.文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
4. 结束语
nginx访问控制模块要数nginx里面最简单的指令,只要记住你想禁止谁访问就deny加上IP,想允许则加上allow ip,想禁止或者允许所有,那么allow all或者deny all即可.文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
站点:运维生存时间 网址:https://www.ttlsa.com/html/3585.html文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/ 文章源自运维生存时间-https://www.ttlsa.com/linux/nginx-modules-ngx_http_access_module/
7F
默认就是allow all的吧
6F
除了IP外,支持域名的黑白名单吗?
5F
恩恩,配置第一个的deny的意义何在?
B1
@ lizhifeng 排除这个ip
B2
@ 默北 不是有deny all 么?
B3
@ lizhifeng 下面有一条allow 192.168.1.0/24的啊
4F
有没有人遇到过一种情况 nginx 做了黑白名单限制,只允许添加的白名单IP访问,其他全部deny,但还是有部分ip能访问网站。这种事情应该从哪里排查,是黑白名单没生效吗?但是有些ip确实又是不能访问的。
3F
[…] 43. nginx向响应内容中追加内容(ngx_http_addition_module模块) 44. nginx访问控制allow、deny(ngx_http_access_module) 45. nginx+perl模块的使用 46. nginx索引目录配置 47. […]
2F
能不能做用户的访问控制
B1
@ 说时依旧_有泪如倾 不行!
1F
嘿,维护站点时,这个非常有必要。
来自外部的引用