Blocking IP Addresses with Nginx


2013-06-29

Nginx is a powerful web server program. I have discussed before the growing popularity of nginx and how to get started with it. This post will build on that and show you how to block and allow specific IP addresses with nginx.

Blocking IP addresses is useful when you want only certain people to access a website. I'm sure it's not a complete block from a security standpoint (throw some kind of authentication in play if you want to be sure), but it works pretty well. To block a certain IP address add this line to the server block in your nginx configuration:

deny 38.114.103.67;

If you are wanting to deny every IP, except a one address, place this line in your server block:

allow 38.114.103.67;
deny all;

Then you will need to reload nginx's configuration for the changes to start working:

nginx -s reload

I usually like to block all IP addresses except my own for certain sites that no one but myself should need to look at.