Blocking IPs with Nginx

How to block IPs

Follow these steps to block an IP address.

  1. View the Nginx configuration file locations article to create your local /nginx/example.com directory.
  2. Create a file named access.conf in this /nginx/example.com directory.
  3. Add the contents from the following sections.
  4. Make sure to reload Nginx for the changes to take effect.

Blocking an IP from hitting your site

location / {
   deny 1.2.3.4;
 }
This blocks the IP address of 1.2.3.4 from accessing your site entirely.

Blocking an IP from hitting a subdirectory

location /subdirectory/ {
   deny 1.2.3.4;
 }

Allowing a single IP while blocking all others

If you want to block access to all IPs while allowing a specific IP to still access your site, use this:

location / {
   allow 9.8.7.6;
   deny all;
 }
This may be helpful if you're working on your site and do not want anyone but you to view it.

Combining rules

You can also create and combine multiple sets of these rules in your access.conf file:

location /subdir {
   allow 1.2.3.4;
   deny all;
 }

 location / {
   deny all;
 }
The above allows 1.2.3.4 to only browse the subdirectory named /subdir. All other IPs are blocked from everywhere in your site.

View the following page for further details:

See also

Did this article answer your questions?

Article last updated .