# Exposed .git and .env in the web root: find and close

> A .git directory, .env file, database dumps and .bak copies are often served over HTTP. How to check your site and close access in Nginx and Apache.

## What actually leaks

These files reach the web root through ordinary work rather than malice: the site was deployed with `git clone`, the settings went into `.env`, a dump was taken before an upgrade, a config was saved "just in case" with a `.bak` extension. None of them is meant for a visitor, but a web server serves whatever sits in the root by default.

The cost varies, but it is always high:

- `.git` — not only the current code but the whole history: deleted passwords, old keys, addresses of internal services.
- `.env` — the database password, payment and mail keys, the application secret, all in one file.
- a database dump — your visitors' personal data in full, password hashes included.
- `.bak`, `.old`, `config.php~` — the editor saved a copy, and it is served as text instead of being executed.

## How to check your own site

The check takes a minute and looks at the status code, not at the look of the page:

```
for p in .git/HEAD .git/config .env .env.bak dump.sql backup.zip config.php.bak; do
  printf '%-18s %s\n' "$p" "$(curl -s -o /dev/null -w '%{http_code}' https://example.com/$p)"
done
```

A 200 is a finding. A 403 or 404 means it is closed. Check separately that the server does not serve the file as text: sometimes `.php.bak` returns 200 with the source inside.

## Nginx: the ban and the precedence trap

The basic rule closes everything starting with a dot and explicitly allows the directory certificate issuance needs:

```
location ^~ /.well-known/acme-challenge/ { allow all; }

location ~ /\. { deny all; return 404; }

location ~* \.(bak|old|orig|save|swp|sql|sql\.gz|tar|tar\.gz|zip|log)$ {
    deny all;
    return 404;
}
```

Now the trap that makes the ban stop working silently. Nginx picks one `location`, and a prefix rule with the `^~` modifier cancels the regular-expression check. If something like `location ^~ /uploads/` appears earlier in the configuration, then inside that path the bans on dots and extensions no longer apply: `/uploads/.git/config` is served again. What you verify is not the presence of a `deny` line in the config, but the status code at the real address.

The second trap is an incomplete list. A ban on one pattern closes neither `composer.lock`, nor the `vendor` directory, nor a `.env.example` that holds real values. Check against a list of what is actually in your directory, not from memory.

## Apache and .htaccess

If the site runs on Apache with `.htaccess` enabled, the ban is written like this:

```
<FilesMatch "^\.|\.(bak|old|orig|save|swp|sql|log)$">
    Require all denied
</FilesMatch>

RedirectMatch 404 /\.git(/|$)
```

There is a trap here too, and a costlier one: when the site is served through Nginx, the `.htaccess` file is not read at all. The rules look written; the ban does not exist. If you are not certain which server serves your static files, determine that from the response header before relying on `.htaccess`.

## The right fix is to move the file out

A ban in the web server is a shield, not an arrangement. It is sounder for the file not to be in the web root at all:

| What | Where it belongs |
| --- | --- |
| The `.git` directory | Outside the site root: deploy by export, not by cloning into the web root |
| Settings holding secrets | One level above the root, or in the service's environment |
| Dumps and archives | A separate directory outside the web root, mode 600 |
| Copies made while editing | Not in the web root: edit locally, deploy the finished file |

## What to do if the file was already served

Closing access is the first step, not the last. If `.env` or a dump was reachable, assume the contents were read: crawlers and scanners walk such addresses constantly. The order is:

1. Close access and move the file out of the web root.
2. Rotate every secret it held: the database password, API keys, the application secret.
3. Check the access log for that address — was there a successful 200, and for whom.
4. If a git history leaked, check it for secrets: that is a job of its own, see [secrets in git history](https://www.zevshost.net/en/kb/sekrety-v-istorii-git-gitleaks/).

## Keeping it from coming back

The status-code check is worth repeating after every deploy — it is cheap and catches exactly what was just added. Neighbouring measures help as well: an [audit of open ports](https://www.zevshost.net/en/kb/otkrytye-porty-servera-audit/) and [scanning the site](https://www.zevshost.net/en/kb/skaner-uyazvimostey-sayta-nuclei-nikto/), which look for such addresses by whole lists.

---

Source: https://www.zevshost.net/en/kb/otkrytyy-git-env-v-vebrute/
Updated: 2026-12-03
