nginx-config-testing
Skill1 starsChanged 13 days agoMITView on GitHub
Use when editing an nginx.conf or default.conf.template, a Dockerfile that serves a built Angular or Vite/React front end with nginx, or a code-generator template that writes one, and when you must prove the config with the real nginx image before shipping. Trigger words - gzip, hashed file caching, no-cache index.html, upstream keepalive, envsubst, template substitution.
In this repository30 other files it gives its agents.
- angular-crud-gotchasSkill
- aws-lightsail-container-deploySkill
- bannerlord-decompileSkill
- codegen-t4-templatesSkill
- csharpSkill
- delphi-code-smellsSkill
- delphi-firedacSkill
- delphiSkill
- delphi-types-and-unitsSkill
- difftasticSkill
- dockerSkill
- efcore-aspnet-reviewSkill
- git-bash-editingSkill
- github-issue-creatorSkill
- microsoft-docsSkill
- minimal-comments-and-docsSkill
- msbuildSkill
- mssqlSkill
- mstest-assertionsSkill
- mysqlSkill
- odata-generated-gridsSkill
- postgresSkill
- powershellSkill
- react-crud-gotchasSkill
- sanitize-for-open-sourceSkill
- self-contained-deploymentSkill
- skill-writerSkill
- ui-conventionsSkill
- verify-in-running-appSkill
- winui3Skill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 60
Check four URLs: a hashed file, an unhashed one (favicon.ico, expect no Cache-Control), index.html, and a router path such as /employees/7 …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
nginx for a built SPA: write it, then run it
The config that worked (compression, caching, keep-alive)
upstream api_backend { server ${API_UPSTREAM}; keepalive 16; } # host:port only, not a URL
server {
listen 80;
root /usr/share/nginx/html; # root at server level, not inside "location /"
gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 5; gzip_min_length 1024;
gzip_types application/javascript text/javascript text/css application/json image/svg+xml text/plain application/xml;
location ~ "-[A-Z0-9]{8}\.(?:js|css|woff2?)$" { # Angular: 8 character hash in the name
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
# Vite/React instead: location /assets/ { same two lines }
location = /index.html { add_header Cache-Control "no-cache"; }
location /api/ {
proxy_pass http://api_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
}
location / { try_files $uri $uri/ /index.html; }
}Rules and traps
- Immutable caching only for files whose name changes with their content. A pattern on extension alone (
png|ico|svg) also catchesfavicon.icoand anything copied frompublic/, which keep their names and would be cached for a year. Angular:-HASH8.js/css/woff2. Vite: everything under/assets/. - A regex
locationcontaining{8}must be quoted (location ~ "...{8}\.js$"); unquoted, nginx reads{as the start of a block andnginx -tfails. Keep the backslash in\.. location = /index.htmlalso covers the SPA fallback:try_files ... /index.htmlis an internal redirect that re-matches locations, so deep links getno-cachetoo.text/htmlis compressed bygzip onwithout listing it.add_headerinside a location replaces the server-level ones; repeat any header you still need there.try_files $uri =404on the hashed-file location turns a missing hashed file into a 404 instead of serving index.html as JavaScript.- Keep-alive to the API needs an
upstreamblock withkeepalive(plus HTTP/1.1 and an emptyConnectionheader) when the nginx is old. nginx 1.31 enables upstream keepalive by default, so a connection-count test cannot tell the two apart on the currentnginx:alpine; the directives are still right for older images. Ifproxy_passis a full URL such as thehttps://host:portAspire injects (${services__timeentryapi__https__0}), there is no upstream block, so only the header half is possible; splitting host and port out of the URL is the real fix. - The official image substitutes only environment variables in
/etc/nginx/templates/*.template(output goes to/etc/nginx/conf.d/), so nginx's own$uri,$hostare safe. The template is included insidehttp {}, soupstreamandservermay sit at its top level. - gzip also compresses the proxied API's JSON when the browser sends
Accept-Encoding: gzip; an API that already setsContent-Encodingis not compressed twice.
Prove it before shipping (Docker Desktop, Git Bash)
export MSYS_NO_PATHCONV=1 # else Git Bash rewrites /etc/... in docker arguments
docker run -d --name t -p 18090:80 -e API_UPSTREAM=api:8080 \
-v "$(cygpath -w $PWD/dist)":/usr/share/nginx/html:ro \
-v "$(cygpath -w $PWD/nginx.conf.template)":/etc/nginx/templates/default.conf.template:ro nginx:alpine
docker exec t nginx -t
curl -sI -H 'Accept-Encoding: gzip' http://localhost:18090/main-XXXXXXXX.js # Content-Encoding + Cache-Control
curl -s -H 'Accept-Encoding: gzip' URL | wc -c ; curl -s URL | wc -c # compressed vs plain bytes- Compare the old and the new config side by side on two ports with the same
dist; the byte counts are the evidence (518,150 to 119,242 for a 520 kB Angular bundle). - Check four URLs: a hashed file, an unhashed one (
favicon.ico, expect no Cache-Control),index.html, and a router path such as/employees/7(expect 200 withno-cache). - To test the proxy half without the real API, put a second
nginx:alpineon a user-defined network serving a JSON file under/srv/api/, and give the front endAPI_UPSTREAM=thatname:8080. - Counting upstream connections:
stub_statuson the API side (location /status { stub_status; }); the third number on line 3 is total accepts. Use127.0.0.1, notlocalhost, inwgetinside the container. Upstream connections are pooled per worker process (18 workers here), so 10 requests show 10 new connections even withkeepalive: send about 200 requests and expect roughly one connection per worker (16 measured) against 200 without a pool. - Pick ports above 18000; Windows reserves ranges (8082 was refused with "forbidden by its access permissions").
curl -D - -o /dev/null -w '%{size_download}'can print 0 under Git Bash; pipe the body intowc -cinstead.
In a code generator template
A template line may not hold two hyphens in a row (CodeGenNew TemplateDashTests), so write comments with one hyphen or none. Assert the directives in a test over the generated text, and extract the generated file between its @@@FILE markers to run it through the real nginx as above.
How this was verified
Tested on this machine. The evidence, the versions used and the items not re-run are in reference/verification.md: read it before saying a claim here was tested, and treat anything listed there as not re-run as unverified.
What this does not prove
- That the config is right for another nginx version or image (only 1.31.8 was tested), or behind a CDN or an ingress.
- That it is safe to ship: it has no TLS, security headers or rate limits.
- That the caching rules suit a build that does not hash its file names the way Angular (8 characters) or Vite (
/assets/) does.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

