Restrict Block Allow IP Addresses in Nginx with Examples

Restrict Block Allow IP Addresses in Nginx with Examples


Restrict Block Allow IP Addresses in Nginx with Examples

NGINX can allow or deny access based on a particular IP address or the range of IP addresses of client computers. To allow or deny access, we need to use the allow and deny directives inside the stream context or a server block.


Table of Contents

  • Allow Deny Syntax
  • Denying All, Except Certain Addresses
  • Deny/Allow a Specific Location

Allow Deny Syntax

The syntax of allowing, deny IP:

server {
  listen 80;

  allow all; # allow all IPs
  deny al; # deny all IPs

  allow IP_ADDRESS; # allow an IP
  allow IP_ADDRESS/24; # allow an IP range

  deny IP_ADDRESS; # deny an IP
  deny IP_ADDRESS/24; # deny an IP range
}

Denying All, Except Certain Addresses

Example of denying all access, except certain addresses:

server {
  listen 80;

  allow 192.168.1.2; # allow single IP
  deny all; # deny all request
}

Deny/Allow a Specific Location

We can deny or allow only a specific location easily:

server {
  listen 80;

  location ^~ /admin { 
    allow 1.2.3.4; 
    deny all;
  }
}

That's all. Thanks for reading. 👍

StarCode Kh

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

Previous Post Next Post
close