How to block/allow IP-addresses in Nginx

Hypernode makes use of Nginx (pronunciation: ‘Engine X’). Nginx performs better than Apache for the same amount of visitores, this allows us to serve your webshop to more visitors than Apache could. Nginx does not use .htaccess files like Apache. This means that configuration previously done in .htaccess files now has to be done in a different format.

Blocking and allowing IP-addresses is done using the access module.

Denying everyone across the site

To deny all access from certain addresses, create a file in /data/web/nginx named server.blacklist, with the following contents:

deny; # Deny a single IP
deny; #Deny a IP range

Denying everyone across the site, except for certain addresses

To deny all access, except certain addresses, add a file named server.whitelist, with the following contents:

allow; # Allow a single remote host
deny all; # Deny everyone else

Denying or allowing only a specific location

To deny access to everybody except certain addresses to a specific directory or request, create a file called server.private-dir containing:

location ^~ /myadmin {
    deny all;
    rewrite / /index.php break;
    echo_exec @phpfpm;

Denying access to your staging environment

If you want to allow only a specific ip address to your staging area, you can add the following snippet to a staging.whitelist, replacing with your base-url and YOURIP with the desired ip address.

if ($http_host ~ "")  {
    set $block_me_now A;
if ($remote_addr != YOURIP) {
    set $block_me_now "${block_me_now}B";
if ($block_me_now = AB) {
    return 403;