Question

Blocking a directory of a website via DNS

Hello, so I have been trying to block only a directory of a website for a few days now. I want to edit it and make it look like the site is telling the user you cannot view this page on this network; like DNS spoofing, but for a directory only.

I’m not very experienced with Nginx or Apache (in Ubuntu) so I’m not quite sure what my options are.

For an example, I want to block example.com/content/iwantthisblocked but I want everything else to be unblocked or “pass through.”

Can anyone help me with this? It would be very helpful as I do not want to spend the money on an expensive web filter.

Thanks!

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted Answer

@AireServ

As far as DNS goes, you won’t be able to block directory access using DNS. DNS simply routes the entries from the DNS provider (i.e. DigitalOcean or your domain registrar) to your web server. Once the request is routed, the web server handles the request and is responsible for how it’s handled (i.e. the response sent to the browser).

You can dig in to more routing options, of course, but for something as simple as blocking directory access, it’d be overkill since NGINX is capable of handling this for you.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

@AireServ

Unless your router allows for the configuration of a destination or redirect, no. Some routers provide more advanced options, some not so much. It all boils down to what the firmware allows for.

@AireServ

Are you wanting to actually block access to the directory, or simply prevent visitors from being able to view the index listing of the directory?

If you want to simply prevent a directory listing, dropping an index.html or index.php file in will prevent others from being able to view a listing of other files (but it will not prevent them from being able to access other files if they happen to guess file names i.e. random_filename.png, for example).

The other methods will require you to modify NGINX configuration, primarily the server block for the website. You’d need to login via SSH and pull up the website server block which should be located in:

/etc/nginx/sites-available

or

/etc/nginx/sites-enabled

Generally, sites-available is symlinked to sites-enabled, so you’d modify the file in the first and the second would be automatically updated.

To block access, you’d want to drop a location block in to the file, which would look something like:

location ~ /folder1 {
    deny all;
}

You’ll want to make sure you add that before other blocks, such as a location block that allows PHP files to be accessed:

location ~ \.php$ {
    ....
}

So, for example, it should look like this:

location ~ /folder1 {
    deny all;
}

location ~ \.php$ {
    ....
}

and not:

location ~ \.php$ {
    ....
}

location ~ /folder1 {
    deny all;
}

You can also block multiple directories using a single block, like so:

location ~ /(folder1|folder2|folder3) {
    deny all;
}

You would simply swap out folder1, folder2, and folder3 with the paths to the folders you want to block.

That being said, the above also will not block access to files, only to the directory itself. So if someone guesses a random filename, they will still be able to access it.

Example:

example.com/content/iwantthisblocked will be denied.

example.com/content/iwantthisblocked/filename.html will not be denied.

I am not sure what the intent is but you could change the permissions on the folder so it’s not accessible to Apache and Apache will generate a 403 forbidden message to the end user.

chmod 000 /path/to/iwanthisblocked

Change mode to 644 if you want it to be accessible.

chmod 755 /path/to/iwanthisblocked

You can customize the 403 message. https://httpd.apache.org/docs/2.4/custom-error.html has details.

Gaurav