Banning IPs with Nginx Rules

Banning IP addresses with Nginx rules can be done at either the server level (all sites on the server) or the site level.

Block at the Server Level

To block at the server level you can use the http-context.conf. To customize this config, run the following command:

nano /etc/nginx/extra.d/http-context.conf

Add your IP as follows (or if multiple IPs, then 1 per line):

deny 199.199.199.19;
deny 123.123.123.45;

Save the file with CTRL+O followed by Enter. Exit nano with CTRL+X.

Now copy the file into the _custom directory to ensure it’s not overwritten:

cp /etc/nginx/extra.d/http-context.conf /etc/nginx/extra.d/_custom/http-context.conf

Now check your Nginx syntax with:

nginx -t

As long as there are no errors present you can then reload Nginx with:

gp ngx reload