What is the meaning of the following location block in Nginx?

location ~ /\.ht {
    deny all;

I ask since I have a small WordPress site and I removed this block from its configuration and restarted the server but the site kept working fine, seemingly.

  • 2
    Keeps people from downloading your security-related files. Only the web server needs to read them, not the general public. – Jeff Schaller Dec 28 '17 at 2:16
location ~ /\.ht {
    deny all;

This directive tells the webserver to deny all incoming requests for any files starting with .ht in the root directory (/).

The tilde ~ tells nginx to use regular expressions.

Thus, files like .htaccess, .htpasswd, etc, will not be served.

Note: The backslash (\) before the dot, is just to escape the dot (the dot that comes before htaccess, htpassword, etc.


Say your website is example.com. The particular location will be example.com/.ht. So the deny all directive will deny all requests to example.com/.ht*. (* indicate any following string)

~ say nginx to go for regular expression based matching. / is root directory of your website. \ is escape character. It says to interpret . literally and not as a part of regular expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.