Block .env & .git in Nginx Without Breaking Routes
Stop bots stealing Laravel .env secrets without killing Let’s Encrypt or app routes. This cheat sheet shows the exact Nginx location order, nginx -t checks, and curl -I proofs Australian site owners can run today.
If you run Laravel or PHP on Nginx in Australia, scanners will hit /.env and /.git/config looking for database passwords and API keys. This guide walks you through a production-safe deny rule that blocks dotfiles, keeps ACME challenges working, and does not break normal routes—plus the checks Fixwebnode uses on remote hardening jobs for sole traders and small operators.
Imprecise regex is the usual failure: a broad “deny all hidden paths” block can return 403 on /.well-known/acme-challenge/, break Certbot renewals, or intercept legitimate URLs. The fix is location order and an anchored pattern. For hands-on remote help, start at Fixwebnode website repair Australia.
Why blocking .env and .git on Nginx matters
Public document roots still ship with leftover .env, .env.backup, or a .git directory after a bad deploy. Bots request those paths continuously. One successful download can expose DB credentials, mail keys, and app secrets. Nginx must deny those requests before they reach PHP-FPM, while still allowing Let’s Encrypt HTTP-01 challenges under /.well-known/.
The reliable pattern is: put a prefix match for well-known first, then an anchored regex that only denies paths starting with a dot at the URL root, then your PHP handler. Test with sudo nginx -t, reload, and verify status codes with curl -I.
How do I block .env and .git in Nginx in Australia without breaking SSL renewals?
Place location ^~ /.well-known/ { allow all; } immediately above an anchored location ~ ^/\. { deny all; } block, and keep both above your PHP location ~ \.php$ handler. Run sudo nginx -t, reload Nginx, then confirm curl -I https://your-domain/.env returns 403 while curl -I https://your-domain/.well-known/acme-challenge/test (or a real challenge path) is not denied by that rule.
That order protects Laravel and PHP sites from credential scraping without stopping Certbot or breaking ordinary app routes that do not begin with a literal dot segment.
| Symptom | Quick fix | When to call Fixwebnode |
|---|---|---|
/.env returns 200 or shows secrets | Add anchored ^/\. deny before PHP; reload | Config spread across includes you cannot safely edit |
| Certbot / SSL renewal fails with 403 | Add ^~ /.well-known/ above the deny block | Renewals still fail after order fix and nginx -t |
| App routes or assets suddenly 403 | Replace loose location ~ /\. with ^/\. only | Multiple vhosts, reverse proxies, or panel overrides |
Common issues when locking down dotfiles
These are the failure modes we see most often on live Laravel and PHP stacks—not theoretical SEO tips.
- Exposed
.envor.gitstill downloadable — scanners get 200/plain text because no deny exists, or the rule sits after PHP and never wins. - Let’s Encrypt renewals break after a “secure” deny-all-dots rule — ACME files under
/.well-known/acme-challenge/hit 403. - Legitimate routes or static paths return 403 — an unanchored regex like
location ~ /\.matches a dot anywhere in the URI. nginx -tpasses but behaviour is wrong — edits went into the wrong server block, a panel-managed include overrides you, or reload never ran.- Only HTTP is protected, HTTPS still serves secrets — duplicate
server { }blocks; the deny was added to port 80 only.
Issue 1 — Bots can still fetch .env or .git
Symptoms: curl -I https://example.com/.env returns 200 or 500 from PHP; response body may show APP_KEY= or DB credentials. Access logs show repeated hits from unknown IPs.
Root cause: Missing deny, deny after the PHP location, or a pattern that does not match root-level dotfiles.
DIY resolution
- SSH to the server and open the active site config (commonly under
/etc/nginx/sites-available/). - Inside the relevant
server { }block, above anylocation ~ \.php$handler, add the well-known allow and the anchored deny:
location ^~ /.well-known/ {
allow all;
# optional: default_type "text/plain";
}
location ~ ^/\. {
deny all;
access_log off;
log_not_found off;
}
- Save the file. Test syntax:
sudo nginx -t
You want syntax is ok and test is successful.
- Reload Nginx (do not restart blindly if you share the host):
sudo systemctl reload nginx
- Verify deny vs allow behaviour:
curl -I https://YOUR_DOMAIN/.env
curl -I https://YOUR_DOMAIN/.git/config
curl -I https://YOUR_DOMAIN/.well-known/acme-challenge/ping
Expect 403 on .env and .git/config. The well-known path should not be blocked by the dotfile rule (you may see 404 if no challenge file exists—that is fine; 403 from your new deny is the failure mode to avoid).
When to book Fixwebnode: You cannot find which server block is live, PHP still serves the file, or a control panel rewrites your vhost on every save.
Issue 2 — Let’s Encrypt / Certbot renewals fail after hardening
Symptoms: certbot renew fails; HTTP-01 validation gets 403; SSL expiry warnings appear in monitoring.
Root cause: A catch-all hidden-file deny matched /.well-known/... because that path starts with a dot segment, and no higher-priority allow existed.
DIY resolution
- Confirm the allow block uses the
^~modifier so it wins over regex locations:
location ^~ /.well-known/ {
allow all;
}
- Place that block immediately above the anchored deny. Order inside the same
serverblock matters:
# 1) ACME / static well-known first
location ^~ /.well-known/ {
allow all;
}
# 2) Deny only URIs whose path begins with /.
location ~ ^/\. {
deny all;
}
# 3) Then PHP / app locations
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
- Test and reload:
sudo nginx -t && sudo systemctl reload nginx
- Dry-run renewal if Certbot is installed:
sudo certbot renew --dry-run
- Spot-check headers:
curl -I http://YOUR_DOMAIN/.well-known/acme-challenge/test-file
curl -I https://YOUR_DOMAIN/.env
When to book Fixwebnode: You use DNS-01 and HTTP-01 mixed across many hosts, or renewal hooks and Nginx includes are tangled after a panel migration.
Issue 3 — Normal routes break after an imprecise regex
Symptoms: Some app URLs, versioned assets, or paths containing . mid-string return 403; only “security” changed recently.
Root cause: Patterns such as location ~ /\. or location ~ /\. without a start anchor match a dot anywhere, not only root-level hidden files like /.env.
DIY resolution
- Search the vhost for overly broad rules:
sudo grep -nE 'location.*\\\.' /etc/nginx/sites-enabled/* /etc/nginx/conf.d/* 2>/dev/null
- Replace loose rules with the anchored form only:
location ~ ^/\. {
deny all;
access_log off;
log_not_found off;
}
- Keep well-known above it. Do not deny
^~ /.as a prefix for everything under the site root unless you fully understand side effects. - Test config and reload:
sudo nginx -t && sudo systemctl reload nginx
- Regression-check real routes and the secrets paths:
curl -I https://YOUR_DOMAIN/
curl -I https://YOUR_DOMAIN/api/health
curl -I https://YOUR_DOMAIN/.env
curl -I https://YOUR_DOMAIN/.git/HEAD
Home and API should be normal app responses (200/301/302 as designed). Dotfiles should be 403.
When to book Fixwebnode: You inherited several includes, map blocks, or a reverse proxy in front of Nginx and cannot tell which layer returns 403.
Issue 4 — nginx -t is clean but curl still shows 200 on .env
Symptoms: Syntax test passes; reload “succeeds”; /.env still served. Sometimes only http:// is fixed while https:// is open.
Root cause: Wrong file edited, duplicate server_name blocks, HTTPS server missing the deny, or another config in sites-enabled wins.
DIY resolution
- Discover which config Nginx actually loaded for the host:
sudo nginx -T 2>/dev/null | grep -nE 'server_name|well-known|deny all|\.env|listen '
- Confirm both port 80 and 443 (or your SSL listen) server blocks contain the same well-known + anchored deny pair, still above PHP.
- Ensure the site is enabled and not shadowed:
ls -l /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
- Verify again with headers only (no body leakage in your terminal history if secrets ever printed):
curl -sI https://YOUR_DOMAIN/.env | head -n 1
curl -sI http://YOUR_DOMAIN/.env | head -n 1
- If the file exists on disk, remove it from the web root after backup (deny is defence in depth; secrets should not live under the public directory):
# Example only — adjust paths to your app
ls -la /var/www/YOUR_APP/public/.env /var/www/YOUR_APP/public/.git 2>/dev/null
Move or delete public copies; keep the real .env outside public/ as Laravel intends.
When to book Fixwebnode: nginx -T output is huge, multiple SSL terminators exist, or you need a full secret-rotation after a confirmed leak.
Cheat sheet: minimal safe snippet order
Use this mental model every time you edit a PHP/Laravel vhost:
location ^~ /.well-known/— allow ACME and similar.location ~ ^/\.— deny root-level dotfiles (.env,.git,.htaccessprobes, etc.).- Static asset locations as needed.
location ~ \.php$— FastCGI to PHP-FPM last among these.
sudo nginx -t
sudo systemctl reload nginx
curl -I https://YOUR_DOMAIN/.env # expect 403
curl -I https://YOUR_DOMAIN/.git/config # expect 403
Optional log skim for scanners:
sudo tail -n 200 /var/log/nginx/access.log | grep -E '\.env|\.git'
When DIY is enough vs when to book Fixwebnode
DIY is enough when you have SSH, a single clear vhost, you can run nginx -t, reload, and prove 403 on dotfiles while renewals and main routes stay healthy. Follow the numbered steps above and keep a copy of the old config before editing.
Book a specialist when any of these apply: you already had a public .env (assume credentials are burned and need rotation), panel-managed Nginx overwrites manual edits, many server blocks or a CDN/proxy disagree on status codes, Certbot still fails after correct location order, or you are not confident editing production config. Fixwebnode works as a direct remote specialist for individuals, sole traders, and local operators across Australia—not a freelance marketplace. See where support is offered on the service areas page.
Talk through your Nginx hardening
If you want a second pair of eyes on location order, SSL renewal health, and whether .env / .git are truly unreachable on both HTTP and HTTPS, start a conversation with Fixwebnode. We work remote-first with Australian small businesses and operators who need practical fixes, not bid boards.
Next step: open https://fixwebnode.com.au/website-repair-australia and describe your stack (Laravel/PHP version, Nginx, whether Certbot is in use). Bring the output of sudo nginx -t and your curl -I results for /.env and /.well-known/ so the review stays concrete.
Lock the well-known allow above the anchored deny, test, reload, and verify 403 versus healthy routes—that is the whole cheat sheet. Do it carefully once, and scanners stop reading your secrets while Let’s Encrypt keeps working.