Nginx map and geo Modules for IP Blocking and Admin Protection

Nginx map and geo: block bad traffic and protect wp-admin before PHP runs

Every request that reaches PHP costs a worker, a WordPress bootstrap and often database queries. Scanners that probe /.env, bots that hammer wp-login.php and scrapers with known user agents can be rejected by Nginx itself in microseconds. Two modules do the classification: geo by client IP, map by any variable such as the user agent or the URI. The result is a variable that a simple if uses to return 403.

Problem: A WordPress server’s access log is full of requests from a handful of networks, from scrapers with known user agents and of probes for /.git/config and /wp-config.php.bak. All of them reach PHP-FPM, and the admin area is reachable from everywhere although the editors work from two offices.

Solution: Define a geo block for blocked networks and another for the office IPs, map blocks for bad user agents and probe URIs, return 403 (or 444, closing the connection) in the server block when one of them matches, and allow wp-login.php and /wp-admin/ only from the office networks, except admin-ajax.php.

The variables in the http block

# /etc/nginx/conf.d/ha-filters.conf
geo $ha_blocked_ip {
    default          0;
    192.0.2.0/24     1;     # example networks, replace with your own list
    198.51.100.17    1;
}

geo $ha_office {
    default          0;
    203.0.113.0/24   1;     # office 1
    2001:db8:10::/48 1;     # office 2, IPv6
}

map $http_user_agent $ha_bad_agent {
    default              0;
    ""                   1;     # no user agent at all
    ~*(sqlmap|nikto|masscan|zgrab) 1;
}

map $request_uri $ha_probe {
    default                            0;
    ~*^/\.(env|git|svn|htpasswd)       1;
    ~*\.(sql|bak|old|orig|swp)(\?|$)   1;
    ~*^/wp-config\.php.+               1;
}

geo matches the client address against networks, the longest prefix wins. map evaluates the source variable lazily, only when the result variable is used. ~* starts a case-insensitive regular expression. These blocks only define variables; nothing is blocked yet.

Use them in the server block

server {
    # ...
    if ( $ha_blocked_ip ) { return 444; }
    if ( $ha_bad_agent )  { return 403; }
    if ( $ha_probe )      { return 404; }

    location = /wp-login.php {
        if ( $ha_office = 0 ) { return 403; }
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location = /wp-admin/admin-ajax.php {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ^~ /wp-admin/ {
        if ( $ha_office = 0 ) { return 403; }
        try_files $uri $uri/ /index.php?$args;
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        }
    }
}

if with only a return inside is the safe use of if in Nginx; the warnings about “if is evil” concern other directives inside it. 444 is an Nginx code that closes the connection without an answer, which costs the least and tells a scanner nothing. Probes get 404 rather than 403, so they do not learn that the file might exist.

admin-ajax.php must stay public: themes and plugins call it from the front end (forms, load more, WooCommerce). It gets its own exact location, which wins over the prefix location of /wp-admin/. Also keep /wp-admin/admin-post.php public if front-end forms post to it. The REST API under /wp-json/ is not affected.

Behind a proxy or CDN

Behind Cloudflare or a load balancer, the client address is the proxy’s. Restore the visitor’s IP with set_real_ip_from and real_ip_header before relying on geo, or use geo $http_x_forwarded_for ... only when the header is set by your own proxy, never trusting it from the internet.

Test and watch

sudo nginx -t && sudo systemctl reload nginx
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/.env                # 404
curl -s -o /dev/null -w "%{http_code}\n" -A "" https://example.com/              # 403
curl -s -o /dev/null -w "%{http_code}\n" https://example.com/wp-login.php        # 403 outside the office

Rate limiting with the same geo/map technique for the login and the REST API: Nginx rate limiting for WordPress.

NOTE: An IP allowlist for the admin locks out editors on the road, password reset links and some plugins that call wp-login.php. Combine it with a VPN, or use it only where the team really works from fixed addresses. Static lists of “bad” IP ranges get old quickly; for automatic blocking based on log patterns, fail2ban with an Nginx action complements these rules. The Apache counterpart: protect wp-config.php and xmlrpc.php via .htaccess.