prevent nginx from .git directory

How to Prevent NGINX from Serving .git directory

NGINX is a popular web server used by millions of organizations and web developers. Often web developers use git version control system to manage their source code and configure NGINX to directory serve files from their git repository. Every git repository has a .git directory that is hidden and contains important information about the repository’s index, head, etc. It should not be exposed publicly by serving over a website, else hackers will be able to get details about your repository. In such cases, it is advisable to prevent NGINX from serving .git directory. In this article, we will learn how to do this.


How to Prevent NGINX from Serving .git directory

Here are the steps to block access to .git directory in NGINX.

1. Open NGINX Configuration File

Open terminal and run the following command to open NGINX configuration file in text editor.

$ sudo vi /etc/nginx/nginx.conf

2. Block Access to .git directory

Add the following code to deny access to .git directory.

location ~ /\.git {
  deny all;
}

The above code snippet will cause NGINX server to return 403 Access Forbidden response when someone tries to access .git directory.

Alternatively, if you want to return 404 Page Not Found response when someone accesses .git directory, modify the above code snippet to return 404 response.

location ~ /\.git {
  return 404;
}

Save and close the NGINX configuration file.

3. Restart NGINX Server

Restart NGINX Server to apply changes.

$ service nginx restart

In this article, we have learnt how to prevent NGINX from serving .git directory. You can use it to block access to .git directory.

Also read:

How to Prevent Apache from Serving .git directory
How to Check if String is Substring of List Items
How to Check if Column is Empty or Null in MySQL
How to Modify MySQL Column to Allow Null
How to Schedule Multiple Cron Jobs in One Crontab

Leave a Reply

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