Score:2

Prevent the .git directory from being server by NGINX

in flag

I'm trying to prevent nginx from serving the .git directory. I've tried a number of suggested things from this site, e.g:

    location ~ /\.(?!well-known).* {
             deny all;
             access_log off;
             log_not_found off;
    }
    location ~ /\.(.*)/?(.*)? {
             deny all;
             return 404;
    }

   location ~ /\.git {
            deny all;
    }

But all of these still allow nginx to serve the config file within the .git directory. A https request to server-name/.git/config is still successful.

The output of nginx -T:

 server {
     server_name umami.server-name.ch;

    location / {
            proxy_pass http://localhost:3000;
            proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/umami.server-name.ch/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/umami.server-name.ch/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}



server {
    if ($host = umami.server-name.ch) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name umami.server-name.ch;
#    listen 80;
    return 404; # managed by Certbot


}

# configuration file /etc/letsencrypt/options-ssl-nginx.conf:
# This file contains important security parameters. If you modify this file
# manually, Certbot will be unable to automatically provide future security
# updates. Instead, Certbot will print and log an error message with a path to
# the up-to-date file that you will need to refer to when manually updating
# this file. Contents are based on https://ssl-config.mozilla.org

ssl_session_cache shared:le_nginx_SSL:10m;
ssl_session_timeout 1440m;
ssl_session_tickets off;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;

ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";

# configuration file /etc/nginx/sites-enabled/www.server-name.ch:
# You may add here your
# server {
#   ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {

    root /var/www/www.server-name.ch/;
    index index.html index.htm index.php;
    # Make site accessible from http://localhost/
    server_name www.server-name.ch server-name.ch;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules

    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

                # With php5-fpm:
                fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
                fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

        # With php5-fpm:
        # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        # fastcgi_pass unix:/var/run/php5-fpm.sock;
        #fastcgi_index index.php;
        #include fastcgi_params;
        #include fastcgi.conf;
    }

        location ~ /\.(?!well-known).* {
                 deny all;
                 access_log off;
                 log_not_found off;
        }
        location ~ /\.(.*)/?(.*)? {
         deny all;
                 return 404;
        }

       location ~ /\.git {
                deny all;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.server-name.ch/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.server-name.ch/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}


server {
   if ($host = www.server-name.ch) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = server-name.ch) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    server_name www.server-name.ch server-name.ch;
    listen 80;
    return 404; # managed by Certbot

    location / {
    if ($http_user_agent ~ (libwww|Wget|LWP|damnBot|BBBike|java|spider|crawl) ) {
       return 403;
   }
}

}
us flag
Please add your full nginx configuration as shown by `nginx -T` to the question.
Maurits avatar
in flag
@TeroKilkanen, thanks, done.
Score:1
in flag

The line with ~ /\.git should be ~ /\.git.* such that is matches to everything coming after.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.