Question

How to deny an IP in Nginx server with Wordpress?

Posted August 28, 2015 3k views
Nginx Ubuntu Firewall

Hi! I have a wordpress page with Ubuntu and Nginx. I know .htaccess does not work with nginx for block IPs.

I try to deny one IP per exemple my mobile ip: 123.123.123.123 in IPTables unsuccessfully with this:

iptables -A INPUT -s 123.123.123.123 -j DROP
iptables -A OUTPUT -s 123.123.123.123 -j DROP
sudo /sbin/iptables-save
I can open my website

I also try with virtual servers in /etc/nginx/sites-available/mydomain.com

server {
listen 80;
root /var/www/html/myfolder;
index index.php index.html index.html
servername mydomain.com www.mydomain.com;
rewrite ^/sitemap
index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemapn=$2 last;
location / {
deny 123.123.123.123;
try
files $uri $uri/ /index.php?q=$uri&$args;
}

then reload / restar Nginx and i also can open my website from my IP....

Is something wrong?
PD:Sorry my English… im from Spain

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

1 answer

I would recommend using ufw instead of iptables directly as it is much more user-friendly. This tutorial will help you get started.

by Shaun Lewis
Learn how to setup a firewall with UFW on an Ubuntu / Debian cloud server.
Submit an Answer