How to disable access to /.git/config file in a docker container

Viewed 112

I have tried to add following config to nginx.conf.

Access to http://ip_address:port/.git is forbidden, while http://ip_address:port/.git/config is still accessible.

server {
    location ~ /\.git
    {
        deny all;
    }
}
1 Answers

You need to anchor the expression to the beginning of the URI with ^:

server {
    # Deny any URI beginning with /.git
    location ~ ^/\.git { deny all; }
}

You can also achieve the same without regex at all:

server {
    # Deny any URI beginning with /.git
    location /.git { deny all; }
}

Using regex makes more sense when you want a general pattern, for example a location for gif, jpg, and jpeg files:

server {
  location ~* \.(gif|jpg|jpeg)$ {
    # some cache settings
  }
}
Related