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.

@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.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.