Help please: nginx deny file access from multiple directories

I have a home webserver running nginx and was wondering how I can deny access to two files that are in multiple directories but still allow access to everything else

multiple directorys inside the main /servers directory contain file1.txt and file2.txt

I need to make a rule to block access to these two files in any directory inside /servers but allow access to all other files inside /servers

thanks

Hi
If you use Apache, you’re all done, because of the .htaccess file already there. If you’re using Nginx, you need to edit your Nginx config to make your website work, because Nginx doesn’t understand .htaccess rules”.

Using Apache and Nginx at the same time is quite uncommon.


This will block all files anywhere that contain that name and extension ( .htaccess )

 <FilesMatch "file1.txt|file2.txt">
     Order Deny,Allow
     Deny From all
    </FilesMatch>

make sure you don’t have a space between|


besides, it would be desirable to have this as well

# Disable Directory Listings in this Directory and Subdirectories
# This will hide the files from the public unless they know direct URLs
Options -Indexes

OR

you modify as needed

https://pceuropa.net/blog/nginx-disallow-file-access/

http://nginx.org/en/docs/http/ngx_http_access_module.html

3 Likes

thanks oxydac
I added this

# block access to console.txt, bans.txt, ladderlog.txt, reports.txt, login_errors.txt
                location ~ console.txt|bans.txt|ladderlog.txt|reports.txt|login_error.txt
                {
                        deny all;
                }

, I got this error
nginx: [emerg] using regex “console.txt|bans.txt|ladderlog.txt|reports.txt|login_error.txt” requires PCRE library in /usr/local/nginx/conf/nginx.conf:77

it looks like I need to get the pcre library and recompile nginx

thanks

1 Like

I just found this nice online tool that converts apache to nginx
https://winginx.com/en/htaccess

2 Likes

I installed the library and recompled nginx and its all good

Thanks

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.