插件窝 干货文章 如何使用Nginx进行HTTP请求的内容过滤和防火墙

如何使用Nginx进行HTTP请求的内容过滤和防火墙

Nginx HTTP ModSecurity 阻止 287    来源:    2025-04-13

使用Nginx进行HTTP请求的内容过滤和防火墙配置

Nginx可以通过多种方式实现HTTP请求的内容过滤和防火墙功能。以下是几种常用的方法:

1. 基于Nginx内置模块的内容过滤

1.1 使用ngx_http_rewrite_module模块

server {
    listen 80;
    server_name example.com;

    # 禁止特定User-Agent
    if ($http_user_agent ~* (wget|curl|libwww-perl)) {
        return 403;
    }

    # 阻止特定URL路径
    location ~* /(admin|backup|config) {
        deny all;
        return 404;
    }

    # 阻止特定查询参数
    if ($query_string ~* "union.*select") {
        return 403;
    }
}

1.2 使用ngx_http_access_module限制IP访问

location /secure/ {
    # 允许特定IP
    allow 192.168.1.0/24;
    allow 10.0.0.1;

    # 拒绝其他所有
    deny all;
}

2. 使用Nginx+Lua实现高级过滤

2.1 安装OpenResty或ngx_lua模块

# 使用OpenResty
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
sudo yum install openresty

2.2 示例Lua脚本过滤

http {
    lua_shared_dict ip_blacklist 10m;

    server {
        listen 80;

        access_by_lua_block {
            local blacklist = ngx.shared.ip_blacklist
            local client_ip = ngx.var.remote_addr

            if blacklist:get(client_ip) then
                ngx.exit(ngx.HTTP_FORBIDDEN)
            end

            -- 检查请求体中的恶意内容
            ngx.req.read_body()
            local body = ngx.req.get_body_data()
            if body and string.find(body:lower(), "<script>") then
                blacklist:set(client_ip, true, 3600) -- 封禁1小时
                ngx.exit(ngx.HTTP_FORBIDDEN)
            end
        }
    }
}

3. 使用ModSecurity WAF

3.1 安装ModSecurity for Nginx

# 在Ubuntu上安装
sudo apt-get install libmodsecurity3 modsecurity-crs
sudo apt-get install nginx-module-modsecurity

3.2 配置ModSecurity

load_module modules/ngx_http_modsecurity_module.so;

http {
    modsecurity on;
    modsecurity_rules_file /etc/nginx/modsec/main.conf;

    server {
        listen 80;

        location / {
            modsecurity on;
            proxy_pass http://backend;
        }
    }
}

4. 使用Nginx的map功能进行过滤

http {
    map $http_user_agent $bad_user_agent {
        default 0;
        "~*bot" 1;
        "~*spider" 1;
        "~*crawler" 1;
    }

    server {
        listen 80;

        if ($bad_user_agent) {
            return 403;
        }
    }
}

5. 限制请求速率

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

    server {
        listen 80;

        location /login {
            limit_req zone=one burst=20 nodelay;
            proxy_pass http://backend;
        }
    }
}

6. 阻止常见攻击模式

server {
    # 阻止SQL注入
    set $block_sql_injections 0;
    if ($query_string ~ "union.*select.*\(") {
        set $block_sql_injections 1;
    }
    if ($block_sql_injections = 1) {
        return 403;
    }

    # 阻止目录遍历
    if ($uri ~* "\.\./") {
        return 403;
    }

    # 阻止XSS攻击
    set $block_xss 0;
    if ($query_string ~ "<script>") {
        set $block_xss 1;
    }
    if ($block_xss = 1) {
        return 403;
    }
}

最佳实践建议

  1. 分层防御:结合多种过滤方法,不要依赖单一机制
  2. 日志记录:记录被阻止的请求以便分析
  3. 定期更新:保持规则库和Nginx版本最新
  4. 性能考虑:复杂的过滤规则可能影响性能,应在测试环境中评估
  5. 灰度发布:新规则应先在小范围测试

通过以上方法,您可以构建一个强大的HTTP请求过滤和防火墙系统,保护您的Web应用免受多种攻击。