IP访问限制使用 ngx_http_access_module 模块,可以在 http
, server
, location
, limit_except 中配置,语法如下:
采用黑名单规则,禁止所有,添加访问IP。
allow IP;
deny all;
想禁止所有虚拟主机的访问,在nginx.conf配置:
include vhost/*.conf;
配置IP文件,允许访问的IP添加文件中。
[root@yunwei-test vhost]# cat blockips.conf.bak # allow all ips allow 10.0.2.174; # block all ips deny all;
只想禁止某一个虚拟主机禁止访问:
# 现在有两个虚拟主机,bbs.qipai.com 和 www.qipai.com 现在想对访问 www.qipai.com 做访问限制。
[root@yunwei-test vhost]# ls bbs.qipai.conf blockips.conf.bak www.qipai.conf
[root@yunwei-test vhost]# cat www.qipai.conf server { listen 80; server_name www.qipai.com; allow 10.0.2.174; # 直接在server区块中配置 deny all; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
这样就对访问 www.qipai.conf 的做了访问限制,访问 bbs.qipai.conf 不受影响。
官方文档:http://nginx.org/en/docs/http/ngx_http_access_module.html