How to block .git directory in nginx

When using git version control to deploy application, many forget to secure .git folder. This allows anyone to clone your git repository. If you have any credentials commited to your git version control, then hacker will be able to gain access.

To avoid this, it is better plan the git repo in a way you have .git folder outside of your document root. If this is not possible, you need to block access to .git folder using nginx configuration.

To block access to .git folder, add following to your nginx server entry.

location ~ /\.git {
  deny all;
}

Now restart nginx

systemctl restart nginx

See Nginx, git

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *