how to block ip public access to my API / Web App

October 3, 2019 194 views
Networking IPv6 Apache

How do I block access to my API / Web App through the public ip of my droplet?

I am using Apache and PHP.

2 Answers

Hi @brdaniellima,

f you are using Name based virtual hosts, you may want to simply block any bots etc that try to access your server by direct IP address. It’s unlikely a real user would try to hit your server by using the actual IP address.

To do this, you can use this Virtual Host config:

<VirtualHost *:80>
    ServerName 123.123.123.123
    Redirect 403 /
    ErrorDocument 403 "No"
    DocumentRoot /dev/null/
    UseCanonicalName Off
    UserDir disabled
</VirtualHost>

Of course, please change the IP to the real server IP address(es)

And that’s it, anyone trying to hit the server by IP address will get a simple 403 error.

Hello,

You could use an .htaccess deny from all rule. For example:

order deny,allow
deny from all
allow from YOUR_IP_HERE

The rule above would deny the access to your site for everyone except your IP address.

If this does not work for any reason, make sure that you’ve got AllowOverride enabled:

https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

Regards,
Bobby

by Etel Sverdlov
This tutorial covers five common uses for the .htaccess file. These include: Mod_Rewrite, Authentication, Custom Error Pages, Mime Types, and Server Side Includes.
Have another answer? Share your knowledge.