How to Block Certain IPs From Accessing Website

Last Updated on 17 June 2023 by Daniel

To block certain IP addresses from accessing your website, you can employ various methods depending on your server configuration. Here are a few common approaches:

  1. .htaccess file (Apache server): If your website is hosted on an Apache web server, you can use the .htaccess file to block specific IP addresses. Create or edit the .htaccess file in the root directory of your website and add the following lines:
order allow,deny
deny from
deny from
allow from all

Replace the IP addresses “” and “” with the actual IP addresses or IP ranges you want to block. Save the file, and the specified IPs will be denied access to your website.

  1. Nginx server configuration: If your website is hosted on an Nginx server, you can block IP addresses using the server configuration file. Locate the configuration file for your website (commonly found in the /etc/nginx/conf.d/ directory) and open it. Inside the server block, add the following lines:
location / {
    allow all;

Modify the IP addresses or IP ranges as needed. Save the file, and Nginx will block the specified IPs from accessing your website.

  1. Firewall rules: If you have a firewall installed on your server (e.g., iptables), you can create rules to block specific IP addresses. The commands required for this approach can vary depending on your operating system and firewall software. Here’s an example using iptables:
iptables -A INPUT -s -j DROP
iptables -A INPUT -s -j DROP

Adjust the IP addresses or ranges accordingly, and execute the commands in the terminal. These rules will prevent the specified IPs from accessing your website.

Remember to test these configurations thoroughly to ensure they work as intended. Additionally, keep in mind that determined individuals can still use methods like VPNs or proxy servers to bypass IP-based blocking.

By Daniel

I'm the founder and CEO of Lionsgate Creative, Password Sentry, and hoodPALS. Besides coding and technology, I also enjoy cycling, photography, and cooking.

Leave a comment