Dec 13, 2014

0 Block & deny IP address with Nginx

How to deny access based on the  IP address with Nginx.

# Create block config file.
touch /etc/nginx/blockips.conf

#Edit nginx.conf file
nano /etc/nginx/nginx.conf

        #Add this line in http section

        ## IP block list  ##
        include blockips.conf;

# Add your unwanted IP's
nano /etc/nginx/blockips.conf

Example : 


# Add configuration for 403 error message

nano /etc/nginx/sites-available/default  [or name of your own]

# In " server { " section add : 

# redirect server error pages to the static page
         error_page   403  /error403.html;
         location = /error403.html {
         root /var/www;
         allow all;

# Create file "error403.html" in directory /var/www
nano /var/www/error403.html

# In error403.html you can add your text 
# Example : 

         <head><title>Error 403 - Access denied!</title></head>
         You do not have access to this page, and you probably know why.

# Change file permissions

sudo chown -R www-data:www-data /var/www/

# Restart nginx
service nginx restart


Post a Comment