Nginx 访问控制

Nginx访问控制指令指南 | Nginx Access Control Guide

基础概念 | Basic Concepts

模块信息 | Module Info

  • 来源ngx_http_access_module模块
    Source: Comes from ngx_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 alldeny all
    Global setting: allow all or deny 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

  1. 顺序敏感:规则按配置顺序执行
    Order-sensitive: Rules execute in configuration order
  2. 默认允许:没有deny all时允许未列IP
    Default allow: Unlisted IPs allowed without deny all
  3. 作用域:可在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

  1. 优先使用防火墙:比Nginx更高效
    Prefer firewall: More efficient than Nginx
  2. 测试环境隔离:限制测试站点访问
    Isolate test env: Restrict test site access
  3. 定期审计:检查规则有效性
    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

(0)
inxk的头像inxk

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信