هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
location ~* \.(?:css|js|woff2|png|jpg|jpeg|webp|avif|svg|ico)$ {
add_header Cache-Control "public, max-age=31536000, immutable" always;
access_log off;
try_files $uri =404;
}
location / {
add_header Cache-Control "no-cache" always;
try_files $uri $uri/ /index.html;
}
gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_types text/css application/javascript application/json image/svg+xml;Step by step
- Long cache only for versioned files —
immutablefor one year is safe only when the file name changes with its content (app.3f9a1c.css), which build tools (Vite, webpack) do. For files without a hash use a shortmax-age(for example 1 hour). - HTML is never cached long —
no-cachemeans "ask the server each time" (cheap withETag/Last-Modified, Nginx sends them for files). So a new deploy is visible at once and points to the new asset names. - Compress text —
gzipfor CSS, JS, JSON, SVG (HTML is always included). Do not gzip images or fonts that are already compressed (PNG, JPG, WebP, WOFF2). - Pre-compressed files — if the build creates
.gzfiles,gzip_static on;serves them without CPU work. - Cache a slow backend — for API answers that are the same for everybody:
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=app:50m max_size=1g inactive=60m use_temp_path=off;
location /api/catalog/ {
proxy_cache app;
proxy_cache_valid 200 10m;
proxy_cache_use_stale error timeout updating;
add_header X-Cache-Status $upstream_cache_status always;
proxy_pass http://127.0.0.1:8080;
}Never cache answers with personal data or Set-Cookie.
- Watch the inheritance —
add_headerin these locations replaces the server-level headers; include your security header snippet there too. - CDN — a CDN in front respects the same
Cache-Controlheaders; purge it after deploys of non-versioned files.
Check that it worked
The asset shows Cache-Control: public, max-age=31536000, immutable; the HTML shows no-cache; curl -sI -H 'Accept-Encoding: gzip' …/app.3f9a1c.css shows Content-Encoding: gzip; for the API, the second request shows X-Cache-Status: HIT. In the browser's network tab, reloads show assets "from memory/disk cache".
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Users see the old site after a deploy | HTML or unversioned files cached long. | no-cache for HTML; long cache only for hashed names. |
Two Cache-Control headers | expires and add_header Cache-Control both used. | Use one of them (only add_header here). |
| Security headers missing on assets | add_header in the location stops inheritance. | Include the security snippet in the location. |
No Content-Encoding: gzip | Type not in gzip_types (or no include mime.types, so CSS is text/plain), file smaller than gzip_min_length, or no Accept-Encoding. | Add the type; keep include mime.types;; test with the header. |
X-Cache-Status always MISS | Backend sends Set-Cookie or Cache-Control: private. | Fix the backend headers, or proxy_ignore_headers only for public data. |
| Cache folder fills the disk | No max_size. | Set max_size and inactive. |
Go further
- Command pages: nginx, curl.
- Next how-tos: Set up Nginx as a reverse proxy, Add security headers in Nginx, Host two websites on one server, Find slow requests in Nginx logs.
- Learn it: Traffic tools at work — Nginx. Full guide: Nginx.
1When is a one-year immutable cache safe?
Only for files whose name changes with their content (a hash in the name), so a new version always has a new URL.
2Why should HTML use no-cache?
The browser checks with the server every time, so after a deploy it gets the new page that points to the new files.