WAF Integration with Reverse Proxy

WAF Integration with Reverse Proxy

Implement Web Application Firewall functionality:

# ModSecurity integration with Nginx
load_module modules/ngx_http_modsecurity_module.so;

http {
    modsecurity on;
    modsecurity_rules_file /etc/nginx/modsec/main.conf;
    
    server {
        location / {
            modsecurity_rules '
                SecRuleEngine On
                SecRequestBodyAccess On
                SecResponseBodyAccess On
                SecRequestBodyLimit 13107200
                
                # SQL Injection Protection
                SecRule ARGS "@detectSQLi" \
                    "id:1001,\
                    phase:2,\
                    block,\
                    msg:\'SQL Injection Attack\',\
                    logdata:\'Matched Data: %{MATCHED_VAR} found within %{MATCHED_VAR_NAME}\',\
                    severity:CRITICAL"
                
                # XSS Protection
                SecRule ARGS|REQUEST_HEADERS|XML:/* "@detectXSS" \
                    "id:1002,\
                    phase:2,\
                    block,\
                    msg:\'XSS Attack\',\
                    severity:CRITICAL"
                
                # Protocol Anomalies
                SecRule REQUEST_METHOD "!@within GET POST PUT DELETE HEAD OPTIONS" \
                    "id:1003,\
                    phase:1,\
                    block,\
                    msg:\'Invalid HTTP Method\',\
                    severity:WARNING"
            ';
            
            proxy_pass http://backend_servers;
        }
    }
}