Nginx访问控制指令指南 | Nginx Access Control Guide
基础概念 | Basic Concepts
模块信息 | Module Info
- 来源:
ngx_http_access_module
模块
Source: Comes fromngx_http_access_module
- 默认:Nginx内置模块
Default: Built-in Nginx module
核心功能 | Core Functions
- 禁止访问:
deny IP地址
Block access:deny IP_ADDRESS
- 允许访问:
allow IP地址
Allow access:allow IP_ADDRESS
- 全局设置:
allow all
或deny all
Global setting:allow all
ordeny all
配置语法 | Configuration Syntax
allow/deny address|CIDR|unix:|all;
参数说明 | Parameters
参数 | 说明 | Parameter | Description |
---|---|---|---|
address | 单个IP地址 | address | Single IP address |
CIDR | IP地址段(如192.168.1.0/24) | CIDR | IP range (e.g. 192.168.1.0/24) |
unix: | Unix域套接字(Nginx 1.5.1+) | unix: | Unix domain sockets |
all | 所有地址 | all | All addresses |
重要特性 | Key Features
- 顺序敏感:规则按配置顺序执行
Order-sensitive: Rules execute in configuration order - 默认允许:没有deny all时允许未列IP
Default allow: Unlisted IPs allowed without deny all - 作用域:可在http/server/location使用
Scope: Usable in http/server/location
实用示例 | Practical Examples
1. 白名单模式 | Whitelist Mode
location /admin {
allow 192.168.1.100;
allow 10.0.0.0/8;
deny all;
}
2. 黑名单模式 | Blacklist Mode
deny 123.45.67.89;
deny 203.0.113.0/24;
# 其他IP自动允许 | Other IPs automatically allowed
3. 混合控制 | Mixed Control
allow 172.16.0.0/12;
deny 172.16.100.5;
allow all;
最佳实践 | Best Practices
- 优先使用防火墙:比Nginx更高效
Prefer firewall: More efficient than Nginx - 测试环境隔离:限制测试站点访问
Isolate test env: Restrict test site access - 定期审计:检查规则有效性
Regular audit: Verify rule effectiveness
测试验证 | Testing Verification
# 测试命令 | Test command
curl -I http://example.com/restricted-path
# 预期返回403表示成功 | Expect 403 response for success
版本检查 | Version Check
nginx -v
# 显示类似 | Output like:
# nginx version: nginx/1.18.0
主题测试文章,只做测试使用。发布者:inxk,转转请注明出处:https://sheegu.com/?p=1