Most nginx problems are not bugs in nginx. They are configs that are perfectly valid, pass nginx -t, and do something slightly different from what the author intended. A missing slash, a variable that is decoded when you expected it raw, an include that picks up a backup file. This article walks through common nginx misconfiguration patterns, explains why each one matters, and shows how to audit your own nginx configuration and fix what you find.

It is a companion to our nginx hardening checklist. That piece covers what to add; this one covers what to look for in configs that already exist. Everything here applies to servers you own or are authorised to review.

How to audit nginx configuration

Start with the real, merged config, not the file you think is loaded:

nginx -T > /tmp/nginx-full.conf 

Then use two complementary methods:

  • Read it with a checklist, top to bottom, focusing on server, location, alias, root, return, rewrite, proxy_pass and include lines.
  • Run a static analyser. Gixy, originally from Yandex, checks nginx configs for several of the patterns below. The original project is no longer maintained; community forks such as gixy-ng and Gixy-Next continue it, so check each fork's activity before you adopt one.

Keep the output of each review, so you can diff the next one.

1. Alias with mismatched slashes

The official example is location /i/ { alias /data/w3/images/; }, with a trailing slash on both sides (core module docs). A common variant drops the slash on the location only:

location /static { alias /var/www/static/; } 

Because /static is a prefix match, request paths that continue straight after the prefix are mapped onto the alias directory in ways you did not plan, which can expose files in the parent directory. Gixy reports this as an alias traversal issue.

Fix: keep slashes consistent on both sides, or use root when the location matches the directory name:

location /static/ { alias /var/www/static/; } 

2. A web root that contains more than the website

root /var/www/app; is fine if that directory only holds public files. It is a problem when it is the whole project: .git, .env, backup archives, composer.json, or log files.

Fix: point root at a public/ subfolder that holds only what should be served, and add a rule that denies dotfiles except .well-known. Then check from outside that /.git/HEAD on your own site returns 403 or 404.

3. $uri in redirects and headers

$uri holds the normalised, decoded URI. Using it in return, rewrite targets or add_header can let encoded control characters in a request end up decoded in a response header, which opens the door to HTTP response splitting. This is the pattern to look for:

return 301 https://$host$uri; 

Fix: use $request_uri, which keeps the original, still-encoded request line:

return 301 https://$host$request_uri; 

Gixy flags this pattern as HTTP splitting.

4. Trusting the Host header

Configs that build URLs from $host or $http_host, combined with no catch-all default server, will happily answer for any hostname a client sends. That can lead to links, redirects or password-reset emails pointing at a domain you do not control, if the application also trusts the header.

Fix: add an explicit default server that returns 444 and rejects unknown TLS handshakes, list real names in server_name, and make the application use a configured base URL rather than the request's Host header.

5. Security headers lost through inheritance

add_header directives are inherited from the parent block only when the current block has none of its own (headers module docs). A single add_header for caching or CORS in one location removes HSTS, CSP and the rest for that location. Without always, error responses do not get them either.

Fix: keep shared headers in one include file, include it wherever a block adds its own header, and add always. Newer nginx (1.29.3 and later) also offers add_header_inherit merge.

6. PHP handlers that run any file

A location like location ~ \.php$ { fastcgi_pass ...; } passes requests to PHP-FPM without checking that the PHP file exists. Combined with writable upload folders, this can lead to uploaded content being executed as code.

Fix: add try_files $uri =404; inside the PHP location so only existing scripts are passed on, and explicitly deny PHP execution in upload directories:

location ~ ^/uploads/.*\.php$ { deny all; } 

7. Status pages and listings left open

stub_status, framework debug pages, /server-status behind a proxy, and autoindex on; on a download folder are all easy to forget. They leak version numbers, internal paths or file names.

Fix: restrict them with allow and deny, or remove them. Search the merged config for autoindex on and stub_status.

8. Over-trusting X-Forwarded-For

With the realip module, set_real_ip_from 0.0.0.0/0; tells nginx to trust a client-supplied header from anyone. Rate limits, allow rules and logs then use an address the client chose.

Fix: list only your load balancer or CDN ranges in set_real_ip_from, and keep that list up to date.

9. Includes that pick up stray files

include /etc/nginx/sites-enabled/*; loads every file in that folder, including site.conf.old or test-copy. That is how a forgotten virtual host or an old, weaker TLS block comes back to life.

Fix: use a pattern such as *.conf, keep backups outside included folders, and review the file list in the nginx -T output.

A quick nginx security misconfiguration review

Check Look for
Alias Slashes match on location and path
Root Points at a public-only folder
Redirects $request_uri, not $uri
Hosts Catch-all default server present
Headers Shared include plus always
PHP try_files $uri =404;, no PHP in uploads
Status pages Restricted or removed
Real IP Only trusted proxy ranges
Includes *.conf patterns, no backups loaded

Verify from outside

Config review tells you what nginx should do. An outside check confirms what it actually does. From another network, request a missing page and check its headers, request your site with an unknown Host header, and confirm that dotfiles and status pages return 403 or 404. Repeat after every change.

For ongoing checks, CerberusGuard discovers hosts and certificates across your domains, alerts on new hosts and expiring certificates, and runs authorised web scans with Nuclei and OWASP ZAP, plus Nmap service detection. Every scan is checked against a signed authorisation for your scope, and one-click retests confirm that a fixed issue stays fixed. Keep an eye on your exposure with CerberusGuard.

Originally published at CerberusGuard blog.